[llvm] Use TargetABI to assign default-target features in getDefaultSubtargetFeatures (PR #100833)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 11 16:49:32 PDT 2024


https://github.com/hiraditya updated https://github.com/llvm/llvm-project/pull/100833

>From 36556aaa83a4d1ba1be44a49ec26c838e3bafdbb Mon Sep 17 00:00:00 2001
From: AdityaK <hiraditya at msn.com>
Date: Fri, 11 Oct 2024 16:00:09 -0700
Subject: [PATCH] Set default target features for autogenerated functions from
 frontend or from the Module level TargetABI

---
 llvm/lib/Transforms/IPO/CrossDSOCFI.cpp   | 30 +++++++++++++++++++++++
 llvm/test/Transforms/CrossDSOCFI/riscv.ll | 19 ++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 llvm/test/Transforms/CrossDSOCFI/riscv.ll

diff --git a/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp b/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
index 91d445dfc4c734..4258de93f360fb 100644
--- a/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
+++ b/llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
@@ -57,6 +57,28 @@ ConstantInt *CrossDSOCFI::extractNumericTypeId(MDNode *MD) {
   return C;
 }
 
+void setDefaultSubtargetFeatures(const Triple &Triple,
+                                 const StringRef TargetABI, Function *F) {
+  if (Triple.isRISCV64()) {
+    if (TargetABI.contains("lp64d"))
+      F->addFnAttr("target-features", "+d");
+    else if (TargetABI.contains("lp64f"))
+      F->addFnAttr("target-features", "+f");
+    else if (TargetABI.contains("lp64q"))
+      F->addFnAttr("target-features", "+q");
+  } else if (Triple.isRISCV32() && TargetABI.contains("ilp32f")) {
+    F->addFnAttr("target-features", "+f");
+  }
+}
+
+StringRef getTargetABIFromMD(const Module &M) {
+  StringRef TargetABI = "";
+  if (auto *TargetABIMD =
+          dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi")))
+    TargetABI = TargetABIMD->getString();
+  return TargetABI;
+}
+
 /// buildCFICheck - emits __cfi_check for the current module.
 void CrossDSOCFI::buildCFICheck(Module &M) {
   // FIXME: verify that __cfi_check ends up near the end of the code section,
@@ -96,6 +118,14 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
   if (T.isARM() || T.isThumb())
     F->addFnAttr("target-features", "+thumb-mode");
 
+  StringRef DefaultTargetFeatures = Ctx.getDefaultTargetFeatures();
+  if (DefaultTargetFeatures.empty()) {
+    auto TargetABI = getTargetABIFromMD(M);
+    setDefaultSubtargetFeatures(T, TargetABI, F);
+  } else {
+    F->addFnAttr("target-features", DefaultTargetFeatures);
+  }
+
   auto args = F->arg_begin();
   Value &CallSiteTypeId = *(args++);
   CallSiteTypeId.setName("CallSiteTypeId");
diff --git a/llvm/test/Transforms/CrossDSOCFI/riscv.ll b/llvm/test/Transforms/CrossDSOCFI/riscv.ll
new file mode 100644
index 00000000000000..9427ff5e8cf0e1
--- /dev/null
+++ b/llvm/test/Transforms/CrossDSOCFI/riscv.ll
@@ -0,0 +1,19 @@
+; RUN: opt -S -passes=cross-dso-cfi < %s | FileCheck --check-prefix=RISCV64 %s
+
+target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
+target triple = "riscv64-unknown-linux-gnu"
+
+define signext i8 @f() !type !0 !type !1 {
+entry:
+  ret i8 1
+}
+
+!llvm.module.flags = !{!2, !3}
+
+!0 = !{i64 0, !"_ZTSFcvE"}
+!1 = !{i64 0, i64 111}
+!2 = !{i32 4, !"Cross-DSO CFI", i32 1}
+!3 = !{i32 1, !"target-abi", !"lp64d"}
+
+; RISCV64: define void @__cfi_check({{.*}} #[[A:.*]] align 4096
+; RISCV64: attributes #[[A]] = { {{.*}}"target-features"="+d"



More information about the llvm-commits mailing list