diff --git a/gcc/config/riscv/multilib-generator b/gcc/config/riscv/multilib-generator
index fe115b3184f..1164d1c5c8e 100755
--- a/gcc/config/riscv/multilib-generator
+++ b/gcc/config/riscv/multilib-generator
@@ -40,6 +40,7 @@ import collections
 import itertools
 from functools import reduce
 import subprocess
+import argparse
 
 #
 # TODO: Add test for this script.
@@ -127,44 +128,69 @@ def expand_combination(ext):
 
   return ext
 
-for cfg in sys.argv[1:]:
-  try:
-    (arch, abi, extra, ext) = cfg.split('-')
-  except:
-    print ("Invalid configure string %s, <arch>-<abi>-<extra>-<extensions>\n"
-           "<extra> and <extensions> can be empty, "
-           "e.g. rv32imafd-ilp32--" % cfg)
-    sys.exit(1)
-
-  arch = arch_canonicalize (arch)
-  arches[arch] = 1
-  abis[abi] = 1
-  extra = list(filter(None, extra.split(',')))
-  ext_combs = expand_combination(ext)
-  alts = sum([[x] + [x + y for y in ext_combs] for x in [arch] + extra], [])
-  alts = list(map(arch_canonicalize, alts))
+multilib_cfgs = filter(lambda x:not x.startswith("--"), sys.argv[1:])
+options = filter(lambda x:x.startswith("--"), sys.argv[1:])
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--cmodel", type=str)
+parser.add_argument("cfgs", type=str, nargs='*')
+args = parser.parse_args()
+
+if args.cmodel:
+  cmodels = [None] + args.cmodel.split(",")
+else:
+  cmodels = [None]
+
+cmodel_options = '/'.join(['mcmodel=%s' % x for x in cmodels[1:]])
+cmodel_dirnames = ' \\\n'.join(cmodels[1:])
+
+for cmodel in cmodels:
+  for cfg in args.cfgs:
+    try:
+      (arch, abi, extra, ext) = cfg.split('-')
+    except:
+      print ("Invalid configure string %s, <arch>-<abi>-<extra>-<extensions>\n"
+             "<extra> and <extensions> can be empty, "
+             "e.g. rv32imafd-ilp32--" % cfg)
+      sys.exit(1)
+
+    # Compact code model only support rv64.
+    if cmodel == "compact" and arch.startswith("rv32"):
+      continue
 
-  # Drop duplicated entry.
-  alts = unique(alts)
+    arch = arch_canonicalize (arch)
+    arches[arch] = 1
+    abis[abi] = 1
+    extra = list(filter(None, extra.split(',')))
+    ext_combs = expand_combination(ext)
+    alts = sum([[x] + [x + y for y in ext_combs] for x in [arch] + extra], [])
+    alts = list(map(arch_canonicalize, alts))
 
-  for alt in alts:
-    if alt == arch:
-      continue
-    arches[alt] = 1
-    reuse.append('march.%s/mabi.%s=march.%s/mabi.%s' % (arch, abi, alt, abi))
-  required.append('march=%s/mabi=%s' % (arch, abi))
+    # Drop duplicated entry.
+    alts = unique(alts)
+
+    for alt in alts[1:]:
+      if alt == arch:
+        continue
+      arches[alt] = 1
+      reuse.append('march.%s/mabi.%s=march.%s/mabi.%s' % (arch, abi, alt, abi))
+
+    if cmodel:
+      required.append('march=%s/mabi=%s/mcmodel=%s' % (arch, abi, cmodel))
+    else:
+      required.append('march=%s/mabi=%s' % (arch, abi))
 
-arch_options = '/'.join(['march=%s' % x for x in arches.keys()])
-arch_dirnames = ' \\\n'.join(arches.keys())
+  arch_options = '/'.join(['march=%s' % x for x in arches.keys()])
+  arch_dirnames = ' \\\n'.join(arches.keys())
 
-abi_options = '/'.join(['mabi=%s' % x for x in abis.keys()])
-abi_dirnames = ' \\\n'.join(abis.keys())
+  abi_options = '/'.join(['mabi=%s' % x for x in abis.keys()])
+  abi_dirnames = ' \\\n'.join(abis.keys())
 
 prog = sys.argv[0].split('/')[-1]
 print('# This file was generated by %s with the command:' % prog)
 print('#  %s' % ' '.join(sys.argv))
 
-print('MULTILIB_OPTIONS = %s %s' % (arch_options, abi_options))
-print('MULTILIB_DIRNAMES = %s %s' % (arch_dirnames, abi_dirnames))
+print('MULTILIB_OPTIONS = %s %s %s' % (arch_options, abi_options, cmodel_options))
+print('MULTILIB_DIRNAMES = %s %s %s' % (arch_dirnames, abi_dirnames, cmodel_dirnames))
 print('MULTILIB_REQUIRED = %s' % ' \\\n'.join(required))
 print('MULTILIB_REUSE = %s' % ' \\\n'.join(reuse))
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 6eee1bb43d4..8e974d2952e 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1328,6 +1328,23 @@ rv64imac with lp64 and rv64imafc with lp64 will reuse this multi-lib set.
 rv64ima-lp64--f,c,fc
 @end smallexample
 
+@option{--with-multilib-generator} have an optional configuration argument
+@option{--cmodel=val} for code model, this option will expand with other
+config options, @var{val} is a comma separated list of possible code model,
+currently we support medlow and medany.
+
+Example 5: Add multi-lib suppport for rv64ima with lp64; rv64ima with lp64 and
+medlow code model
+@smallexample
+rv64ima-lp64--;--cmodel=medlow
+@end smallexample
+
+Example 6: Add multi-lib suppport for rv64ima with lp64; rv64ima with lp64 and
+medlow code model; rv64ima with lp64 and medany code model
+@smallexample
+rv64ima-lp64--;--cmodel=medlow,medany
+@end smallexample
+
 @item --with-endian=@var{endians}
 Specify what endians to use.
 Currently only implemented for sh*-*-*.
