[clang] [X86] Add regcall4 attribute to make a specific function respect regc… (PR #69628)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 11:34:49 PDT 2023


https://github.com/yubingex007-a11y created https://github.com/llvm/llvm-project/pull/69628

…all ABIv4

>From 786b954e621ac53902ceff4640d1372ef1652699 Mon Sep 17 00:00:00 2001
From: Bing1 Yu <bing1.yu at intel.com>
Date: Fri, 20 Oct 2023 02:33:35 +0800
Subject: [PATCH] [X86] Add regcall4 attribute to make a specific function
 respect regcall ABIv4

---
 clang/include/clang/Basic/Attr.td     | 5 +++++
 clang/lib/AST/TypePrinter.cpp         | 1 +
 clang/lib/CodeGen/CGCall.cpp          | 3 ++-
 clang/lib/CodeGen/CodeGenModule.cpp   | 3 +++
 clang/test/CodeGen/regcall.c          | 2 +-
 llvm/lib/Target/X86/X86CallingConv.td | 2 +-
 6 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 5486b36133755cc..54d6d0619223f98 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1524,6 +1524,11 @@ def RegCall : DeclOrTypeAttr {
   let Documentation = [RegCallDocs];
 }
 
+def RegCall4 : DeclOrTypeAttr {
+  let Spellings = [GCC<"regcall4">, CustomKeyword<"__regcall4">];
+  let Documentation = [RegCallDocs];
+}
+
 def Final : InheritableAttr {
   let Spellings = [CustomKeyword<"final">, CustomKeyword<"sealed">];
   let Accessors = [Accessor<"isSpelledAsSealed", [CustomKeyword<"sealed">]>];
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index b9f6c0eeb450d2c..6ca95eefe4b0630 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -1861,6 +1861,7 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
   case attr::MSABI: OS << "ms_abi"; break;
   case attr::SysVABI: OS << "sysv_abi"; break;
   case attr::RegCall: OS << "regcall"; break;
+  case attr::RegCall4: OS << "regcall4"; break;
   case attr::Pcs: {
     OS << "pcs(";
    QualType t = T->getEquivalentType();
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 150450e9165900f..82d25a98a271db9 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2458,7 +2458,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
   // * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>
   // * FunctionDecl attributes: __attribute__((no_builtin(...)))
   addNoBuiltinAttributes(FuncAttrs, getLangOpts(), NBA);
-
+  if (TargetDecl->hasAttr<RegCall4Attr>())
+    FuncAttrs.addAttribute("regcall4");
   // Collect function IR attributes based on global settiings.
   getDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
 
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index b1a6683a66bd052..505167f5706fbaf 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2317,6 +2317,9 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
     return;
   }
 
+  if (D->hasAttr<RegCall4Attr>())
+    B.addAttribute("regcall4");
+
   // Handle SME attributes that apply to function definitions,
   // rather than to function prototypes.
   if (D->hasAttr<ArmLocallyStreamingAttr>())
diff --git a/clang/test/CodeGen/regcall.c b/clang/test/CodeGen/regcall.c
index f10da87353fa114..dc37c0545f27389 100644
--- a/clang/test/CodeGen/regcall.c
+++ b/clang/test/CodeGen/regcall.c
@@ -9,7 +9,7 @@ void __regcall v1(int a, int b) {}
 // X86: define dso_local x86_regcallcc void @__regcall3__v1(i32 inreg noundef %a, i32 inreg noundef %b)
 // X64: define dso_local x86_regcallcc void @__regcall3__v1(i32 noundef %a, i32 noundef %b)
 
-void __attribute__((regcall)) v1b(int a, int b) {}
+void __attribute__((regcall4)) v1b(int a, int b) {}
 // X86: define dso_local x86_regcallcc void @__regcall3__v1b(i32 inreg noundef %a, i32 inreg noundef %b)
 // X64: define dso_local x86_regcallcc void @__regcall3__v1b(i32 noundef %a, i32 noundef %b)
 
diff --git a/llvm/lib/Target/X86/X86CallingConv.td b/llvm/lib/Target/X86/X86CallingConv.td
index 19a295cd109627e..df9f48c777cfa8b 100644
--- a/llvm/lib/Target/X86/X86CallingConv.td
+++ b/llvm/lib/Target/X86/X86CallingConv.td
@@ -25,7 +25,7 @@ class CCIfNotSubtarget<string F, CCAction A>
 
 /// CCIfRegCallv4 - Match if RegCall ABIv4 is respected.
 class CCIfRegCallv4<CCAction A>
-    : CCIf<"State.getMachineFunction().getFunction().getParent()->getModuleFlag(\"RegCallv4\")!=nullptr",
+    : CCIf<"State.getMachineFunction().getFunction().getParent()->getModuleFlag(\"RegCallv4\")!=nullptr || State.getMachineFunction().getFunction().hasFnAttribute("regcall4")",
            A>;
 
 /// CCIfIsVarArgOnWin - Match if isVarArg on Windows 32bits.



More information about the cfe-commits mailing list