[clang] [RISCV] Allow -mcmodel= to accept large for RV64 (PR #107817)

Jim Lin via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 10 18:48:03 PDT 2024


https://github.com/tclin914 updated https://github.com/llvm/llvm-project/pull/107817

>From e394e7ca9e769deb3f286f53a48a049340bd51bd Mon Sep 17 00:00:00 2001
From: Jim Lin <jim at andestech.com>
Date: Mon, 9 Sep 2024 13:09:23 +0800
Subject: [PATCH 1/3] [RISCV] Allow -mcmodel= to accept large for RV64

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 3 ++-
 clang/test/Driver/riscv-mcmodel.c          | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 2ce6779f4b43e3..94e4fdd58e692a 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2906,7 +2906,8 @@ void tools::addMCModel(const Driver &D, const llvm::opt::ArgList &Args,
         CM = "small";
       else if (CM == "medany")
         CM = "medium";
-      Ok = CM == "small" || CM == "medium";
+      Ok = CM == "small" || CM == "medium" ||
+           (CM == "large" && Triple.isRISCV64());
     } else if (Triple.getArch() == llvm::Triple::x86_64) {
       Ok = llvm::is_contained({"small", "kernel", "medium", "large", "tiny"},
                               CM);
diff --git a/clang/test/Driver/riscv-mcmodel.c b/clang/test/Driver/riscv-mcmodel.c
index 4f5fa95f59b666..2482672d625fe3 100644
--- a/clang/test/Driver/riscv-mcmodel.c
+++ b/clang/test/Driver/riscv-mcmodel.c
@@ -10,5 +10,11 @@
 // RUN: %clang --target=riscv32 -### -c -mcmodel=medany %s 2>&1 | FileCheck --check-prefix=MEDIUM %s
 // RUN: %clang --target=riscv64 -### -c -mcmodel=medany %s 2>&1 | FileCheck --check-prefix=MEDIUM %s
 
+// RUN: not %clang --target=riscv32 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=ERR-LARGE %s
+// RUN: %clang --target=riscv64 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=LARGE %s
+
 // SMALL: "-mcmodel=small"
 // MEDIUM: "-mcmodel=medium"
+// LARGE: "-mcmodel=large"
+
+// ERR-LARGE:  error: unsupported argument 'large' to option '-mcmodel=' for target 'riscv32'

>From 5361de735b5dc67e476163f73f2bd05f15f8d0c9 Mon Sep 17 00:00:00 2001
From: Jim Lin <jim at andestech.com>
Date: Tue, 10 Sep 2024 10:35:48 +0800
Subject: [PATCH 2/3] Add checking that large code model is disallowed to be
 used with PIC code model.

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 4 ++++
 clang/test/Driver/riscv-mcmodel.c          | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 94e4fdd58e692a..f58b816a9709dd 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2902,6 +2902,10 @@ void tools::addMCModel(const Driver &D, const llvm::opt::ArgList &Args,
     } else if (Triple.isPPC64() || Triple.isOSAIX()) {
       Ok = CM == "small" || CM == "medium" || CM == "large";
     } else if (Triple.isRISCV()) {
+      // Large code model is disallowed to be used with PIC code model.
+      if (CM == "large" && RelocationModel != llvm::Reloc::Static)
+        D.Diag(diag::err_drv_argument_not_allowed_with)
+            << A->getAsString(Args) << "-fpic";
       if (CM == "medlow")
         CM = "small";
       else if (CM == "medany")
diff --git a/clang/test/Driver/riscv-mcmodel.c b/clang/test/Driver/riscv-mcmodel.c
index 2482672d625fe3..c27d7c63a75a4f 100644
--- a/clang/test/Driver/riscv-mcmodel.c
+++ b/clang/test/Driver/riscv-mcmodel.c
@@ -13,8 +13,11 @@
 // RUN: not %clang --target=riscv32 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=ERR-LARGE %s
 // RUN: %clang --target=riscv64 -### -c -mcmodel=large %s 2>&1 | FileCheck --check-prefix=LARGE %s
 
+// RUN: not %clang --target=riscv64 -### -c -mcmodel=large -fpic %s 2>&1 | FileCheck --check-prefix=LARGE %s
+
 // SMALL: "-mcmodel=small"
 // MEDIUM: "-mcmodel=medium"
 // LARGE: "-mcmodel=large"
 
 // ERR-LARGE:  error: unsupported argument 'large' to option '-mcmodel=' for target 'riscv32'
+// ERR-PIC-LARGE:  error: invalid argument '-mcmodel=large' not allowed with '-fpic'

>From cb5e9464f0b1d7a637b6ce6e8c3cff38be52ca3d Mon Sep 17 00:00:00 2001
From: Jim Lin <jim at andestech.com>
Date: Wed, 11 Sep 2024 09:29:43 +0800
Subject: [PATCH 3/3] Add -mcmodel=large to ReleaseNotes.rst

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 250821a9f9c45c..996dfcba0f35eb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -455,6 +455,8 @@ LoongArch Support
 RISC-V Support
 ^^^^^^^^^^^^^^
 
+- The option ``-mcmodel=large`` for the large code model is supported.
+
 CUDA/HIP Language Changes
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 



More information about the cfe-commits mailing list