[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 12:21:55 PDT 2023


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

>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 1/3] [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.

>From ed514a91cdc693242edd01356b6786b6e70d2343 Mon Sep 17 00:00:00 2001
From: Bing1 Yu <bing1.yu at intel.com>
Date: Fri, 20 Oct 2023 03:09:24 +0800
Subject: [PATCH 2/3] fix

---
 clang/lib/CodeGen/CGCall.cpp          | 4 ++--
 clang/lib/Sema/SemaDeclAttr.cpp       | 3 +++
 llvm/lib/Target/X86/X86CallingConv.td | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 82d25a98a271db9..140091bbaac2a89 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2412,6 +2412,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
       FuncAttrs.addAttribute("no_caller_saved_registers");
     if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())
       FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);
+    if (TargetDecl->hasAttr<RegCall4Attr>())
+      FuncAttrs.addAttribute("regcall4");
     if (TargetDecl->hasAttr<LeafAttr>())
       FuncAttrs.addAttribute(llvm::Attribute::NoCallback);
 
@@ -2458,8 +2460,6 @@ 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/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 5adf058bea56a53..3edd06127652a20 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -9638,6 +9638,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
 
   case ParsedAttr::AT_UsingIfExists:
     handleSimpleAttribute<UsingIfExistsAttr>(S, D, AL);
+
+  case ParsedAttr::AT_RegCall4:
+    handleSimpleAttribute<RegCall4Attr>(S, D, AL);
     break;
   }
 }
diff --git a/llvm/lib/Target/X86/X86CallingConv.td b/llvm/lib/Target/X86/X86CallingConv.td
index df9f48c777cfa8b..99b2fc704206987 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 || State.getMachineFunction().getFunction().hasFnAttribute("regcall4")",
+    : CCIf<"State.getMachineFunction().getFunction().getParent()->getModuleFlag(\"RegCallv4\")!=nullptr || State.getMachineFunction().getFunction().hasFnAttribute(\"regcall4\")",
            A>;
 
 /// CCIfIsVarArgOnWin - Match if isVarArg on Windows 32bits.

>From 12f348a3f4a8e90f1ebf5cf1ad51b2b0533add40 Mon Sep 17 00:00:00 2001
From: Bing1 Yu <bing1.yu at intel.com>
Date: Fri, 20 Oct 2023 03:20:58 +0800
Subject: [PATCH 3/3] add mangling

---
 clang/lib/CodeGen/CodeGenModule.cpp | 2 +-
 clang/test/CodeGen/regcall.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 505167f5706fbaf..c6d3cd89d725423 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1742,7 +1742,7 @@ static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD,
 
     if (FD &&
         FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
-      if (CGM.getLangOpts().RegCall4)
+      if (CGM.getLangOpts().RegCall4 || FD->hasAttr<RegCall4Attr>())
         Out << "__regcall4__" << II->getName();
       else
         Out << "__regcall3__" << II->getName();
diff --git a/clang/test/CodeGen/regcall.c b/clang/test/CodeGen/regcall.c
index dc37c0545f27389..ee8372c6f1312e1 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__((regcall4)) v1b(int a, int b) {}
+void __attribute__((regcall4, regcall)) 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)
 



More information about the cfe-commits mailing list