[SPIRV] Fix call lowering of "anonymous" functions
authorMichal Paszkowski <michal.paszkowski@outlook.com>
Thu, 6 Oct 2022 19:35:41 +0000 (21:35 +0200)
committerMichal Paszkowski <michal.paszkowski@outlook.com>
Mon, 10 Oct 2022 22:06:29 +0000 (00:06 +0200)
The patch fixes lowering of anonymous functions, removes file/linkage
info for builtin call demangling, and adds relevant test demonstrating
a fixed problem.

Differential Revision: https://reviews.llvm.org/D135390

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
llvm/lib/Target/SPIRV/SPIRVMCInstLower.cpp
llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h
llvm/lib/Target/SPIRV/SPIRVUtils.cpp
llvm/lib/Target/SPIRV/SPIRVUtils.h
llvm/test/CodeGen/SPIRV/function/internal-anonymous-function.ll [new file with mode: 0644]

index 13415f2..f32d70e 100644 (file)
@@ -394,7 +394,7 @@ static void addOpsFromMDNode(MDNode *MDN, MCInst &Inst,
       if (ConstantInt *Const = dyn_cast<ConstantInt>(C)) {
         Inst.addOperand(MCOperand::createImm(Const->getZExtValue()));
       } else if (auto *CE = dyn_cast<Function>(C)) {
-        Register FuncReg = MAI->getFuncReg(CE->getName().str());
+        Register FuncReg = MAI->getFuncReg(CE);
         assert(FuncReg.isValid());
         Inst.addOperand(MCOperand::createReg(FuncReg));
       }
@@ -426,7 +426,7 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
     const Function &F = *FI;
     if (F.isDeclaration())
       continue;
-    Register FReg = MAI->getFuncReg(F.getGlobalIdentifier());
+    Register FReg = MAI->getFuncReg(&F);
     assert(FReg.isValid());
     if (MDNode *Node = F.getMetadata("reqd_work_group_size"))
       outputExecutionModeFromMDNode(FReg, Node,
@@ -464,9 +464,9 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
       // the annotated variable.
       Value *AnnotatedVar = CS->getOperand(0)->stripPointerCasts();
       if (!isa<Function>(AnnotatedVar))
-        llvm_unreachable("Unsupported value in llvm.global.annotations");
+        report_fatal_error("Unsupported value in llvm.global.annotations");
       Function *Func = cast<Function>(AnnotatedVar);
-      Register Reg = MAI->getFuncReg(Func->getGlobalIdentifier());
+      Register Reg = MAI->getFuncReg(Func);
 
       // The second field contains a pointer to a global annotation string.
       GlobalVariable *GV =
index 18193bf..9cd32cc 100644 (file)
@@ -374,7 +374,7 @@ bool SPIRVCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
 
   Register ResVReg =
       Info.OrigRet.Regs.empty() ? Register(0) : Info.OrigRet.Regs[0];
-  std::string FuncName = Info.Callee.getGlobal()->getGlobalIdentifier();
+  std::string FuncName = Info.Callee.getGlobal()->getName().str();
   std::string DemangledName = getOclOrSpirvBuiltinDemangledName(FuncName);
   const auto *ST = static_cast<const SPIRVSubtarget *>(&MF.getSubtarget());
   // TODO: check that it's OCL builtin, then apply OpenCL_std.
index 8aaac50..0fa05d3 100644 (file)
@@ -31,7 +31,7 @@ void SPIRVMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI,
     default:
       llvm_unreachable("unknown operand type");
     case MachineOperand::MO_GlobalAddress: {
-      Register FuncReg = MAI->getFuncReg(MO.getGlobal()->getGlobalIdentifier());
+      Register FuncReg = MAI->getFuncReg(dyn_cast<Function>(MO.getGlobal()));
       assert(FuncReg.isValid() && "Cannot find function Id");
       MCOp = MCOperand::createReg(FuncReg);
       break;
index bc3f234..d7f6205 100644 (file)
@@ -304,7 +304,7 @@ void SPIRVModuleAnalysis::collectFuncNames(MachineInstr &MI,
     Register GlobalReg = MAI.getRegisterAlias(MI.getMF(), Reg);
     assert(GlobalReg.isValid());
     // TODO: check that it does not conflict with existing entries.
-    MAI.FuncNameMap[F.getGlobalIdentifier()] = GlobalReg;
+    MAI.FuncNameMap[getFunctionGlobalIdentifier(&F)] = GlobalReg;
   }
 }
 
index be7c1a4..2cbec9b 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "MCTargetDesc/SPIRVBaseInfo.h"
 #include "SPIRVGlobalRegistry.h"
+#include "SPIRVUtils.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallSet.h"
@@ -150,8 +151,9 @@ struct ModuleAnalysisInfo {
   // The table maps MBB number to SPIR-V unique ID register.
   DenseMap<int, Register> BBNumToRegMap;
 
-  Register getFuncReg(std::string FuncName) {
-    auto FuncReg = FuncNameMap.find(FuncName);
+  Register getFuncReg(const Function *F) {
+    assert(F && "Function is null");
+    auto FuncReg = FuncNameMap.find(getFunctionGlobalIdentifier(F));
     assert(FuncReg != FuncNameMap.end() && "Cannot find function Id");
     return FuncReg->second;
   }
index 9eeccb5..07bcdbd 100644 (file)
@@ -351,4 +351,11 @@ bool isSpecialOpaqueType(const Type *Ty) {
     return isOpenCLBuiltinType(SType) || isSPIRVBuiltinType(SType);
   return false;
 }
+
+std::string getFunctionGlobalIdentifier(const Function *F) {
+  StringRef Name = F->hasName() ? F->getName() : ".anonymous";
+  GlobalValue::LinkageTypes Linkage = F->getLinkage();
+  StringRef ModuleFileName = F->getParent()->getSourceFileName();
+  return GlobalValue::getGlobalIdentifier(Name, Linkage, ModuleFileName);
+}
 } // namespace llvm
index 5006cae..e4e07dc 100644 (file)
@@ -90,5 +90,7 @@ std::string getOclOrSpirvBuiltinDemangledName(StringRef Name);
 
 // Check if given LLVM type is a special opaque builtin type.
 bool isSpecialOpaqueType(const Type *Ty);
+
+std::string getFunctionGlobalIdentifier(const Function *F);
 } // namespace llvm
 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
diff --git a/llvm/test/CodeGen/SPIRV/function/internal-anonymous-function.ll b/llvm/test/CodeGen/SPIRV/function/internal-anonymous-function.ll
new file mode 100644 (file)
index 0000000..7610919
--- /dev/null
@@ -0,0 +1,20 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+
+;; Types:
+; CHECK-DAG:  %[[#F32:]] = OpTypeFloat 32
+; CHECK-DAG:  %[[#FNF32:]] = OpTypeFunction %[[#F32]] %[[#F32]]
+;; Function decl:
+; CHECK:      %[[#ANON:]] = OpFunction %[[#F32]] None %[[#FNF32]]
+; CHECK-NEXT: OpFunctionParameter %[[#F32]]
+; CHECK-NEXT: OpLabel
+; CHECK-NEXT: OpReturnValue
+; CHECK-NEXT: OpFunctionEnd
+define internal spir_func float @0(float %a) {
+  ret float %a
+}
+
+; CHECK:      OpFunctionCall %[[#F32]] %[[#ANON]]
+define spir_kernel void @foo(float %a) {
+  %1 = call spir_func float @0(float %a)
+  ret void
+}