diff --git a/clang/lib/Basic/Targets/RISCV.cpp b/clang/lib/Basic/Targets/RISCV.cpp index a4925e84784af9..9382645d2c7110 100644 --- a/clang/lib/Basic/Targets/RISCV.cpp +++ b/clang/lib/Basic/Targets/RISCV.cpp @@ -224,6 +224,26 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts, else Builder.defineMacro("__riscv_32e"); } + + if (ISAInfo->hasExtension("zicfilp") && Opts.CFProtectionBranch) { + auto Scheme = Opts.getCFBranchLabelScheme(); + if (Scheme == CFBranchLabelSchemeKind::Default) + Scheme = getDefaultCFBranchLabelScheme(); + + switch (Scheme) { + case CFBranchLabelSchemeKind::Unlabeled: + Builder.defineMacro("__riscv_landing_pad", "1"); + Builder.defineMacro("__riscv_landing_pad_unlabeled", "1"); + break; + case CFBranchLabelSchemeKind::Default: + llvm_unreachable("default cf-branch-label scheme should already be " + "transformed to other scheme"); + case CFBranchLabelSchemeKind::FuncSig: + Builder.defineMacro("__riscv_landing_pad", "1"); + Builder.defineMacro("__riscv_landing_pad_func_sig", "1"); + break; + } + } } static constexpr Builtin::Info BuiltinInfo[] = { diff --git a/clang/test/Preprocessor/riscv-target-features.c b/clang/test/Preprocessor/riscv-target-features.c index 60675065495bba..ea2d9dd8500f6e 100644 --- a/clang/test/Preprocessor/riscv-target-features.c +++ b/clang/test/Preprocessor/riscv-target-features.c @@ -1781,3 +1781,35 @@ // RUN: %clang --target=riscv64-unknown-linux-gnu -mcpu=sifive-p450 -E -dM %s \ // RUN: -o - | FileCheck %s --check-prefix=CHECK-MISALIGNED-FAST // CHECK-MISALIGNED-FAST: __riscv_misaligned_fast 1 + +// Landing Pad + +// RUN: %clang --target=riscv32 -menable-experimental-extensions \ +// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -fcf-branch-label-scheme=unlabeled -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s +// RUN: %clang --target=riscv64 -menable-experimental-extensions \ +// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -fcf-branch-label-scheme=unlabeled -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s +// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}} +// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}} + +// Default cf-branch-label-scheme is func-sig +// RUN: %clang --target=riscv32 -menable-experimental-extensions \ +// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s +// RUN: %clang --target=riscv64 -menable-experimental-extensions \ +// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s + +// RUN: %clang --target=riscv32 -menable-experimental-extensions \ +// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -fcf-branch-label-scheme=func-sig -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s +// RUN: %clang --target=riscv64 -menable-experimental-extensions \ +// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -fcf-branch-label-scheme=func-sig -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s +// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}} +// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}