From 4f5495d1369407d0798c577d0c47d5d2ce990d45 Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Tue, 17 Sep 2019 08:09:56 +0000 Subject: [PATCH] [RISCV] Define __riscv_cmodel_medlow and __riscv_cmodel_medany correctly RISC-V LLVM was only implement small/medlow code model, so it defined __riscv_cmodel_medlow directly without check. Now, we have medium/medany code model in RISC-V back-end, it should define according the actually code model. Reviewed By: lewis-revill Differential Revision: https://reviews.llvm.org/D67065 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@372078 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Basic/Targets/RISCV.cpp | 10 ++++++++-- test/Preprocessor/riscv-cmodel.c | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/Preprocessor/riscv-cmodel.c diff --git a/lib/Basic/Targets/RISCV.cpp b/lib/Basic/Targets/RISCV.cpp index 0a8df86fc8..ab8272c034 100644 --- a/lib/Basic/Targets/RISCV.cpp +++ b/lib/Basic/Targets/RISCV.cpp @@ -88,8 +88,14 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts, Builder.defineMacro("__riscv"); bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64; Builder.defineMacro("__riscv_xlen", Is64Bit ? "64" : "32"); - // TODO: modify when more code models are supported. - Builder.defineMacro("__riscv_cmodel_medlow"); + StringRef CodeModel = getTargetOpts().CodeModel; + if (CodeModel == "default") + CodeModel = "small"; + + if (CodeModel == "small") + Builder.defineMacro("__riscv_cmodel_medlow"); + else if (CodeModel == "medium") + Builder.defineMacro("__riscv_cmodel_medany"); StringRef ABIName = getABI(); if (ABIName == "ilp32f" || ABIName == "lp64f") diff --git a/test/Preprocessor/riscv-cmodel.c b/test/Preprocessor/riscv-cmodel.c new file mode 100644 index 0000000000..b28c27c9cc --- /dev/null +++ b/test/Preprocessor/riscv-cmodel.c @@ -0,0 +1,20 @@ +// RUN: %clang -target riscv32-unknown-linux-gnu -march=rv32i -x c -E -dM %s \ +// RUN: -o - | FileCheck --check-prefix=CHECK-MEDLOW %s +// RUN: %clang -target riscv64-unknown-linux-gnu -march=rv64i -x c -E -dM %s \ +// RUN: -o - | FileCheck --check-prefix=CHECK-MEDLOW %s + +// RUN: %clang -target riscv32-unknown-linux-gnu -march=rv32i -x c -E -dM %s \ +// RUN: -mcmodel=small -o - | FileCheck --check-prefix=CHECK-MEDLOW %s +// RUN: %clang -target riscv64-unknown-linux-gnu -march=rv64i -x c -E -dM %s \ +// RUN: -mcmodel=small -o - | FileCheck --check-prefix=CHECK-MEDLOW %s + +// CHECK-MEDLOW: #define __riscv_cmodel_medlow 1 +// CHECK-MEDLOW-NOT: __riscv_cmodel_medany + +// RUN: %clang -target riscv32-unknown-linux-gnu -march=rv32i -x c -E -dM %s \ +// RUN: -mcmodel=medium -o - | FileCheck --check-prefix=CHECK-MEDANY %s +// RUN: %clang -target riscv64-unknown-linux-gnu -march=rv64i -x c -E -dM %s \ +// RUN: -mcmodel=medium -o - | FileCheck --check-prefix=CHECK-MEDANY %s + +// CHECK-MEDANY: #define __riscv_cmodel_medany 1 +// CHECK-MEDANY-NOT: __riscv_cmodel_medlow -- 2.50.1