[llvm] [SPIR-V] Introduce support for 'spirv.Decorations' metadata node in SPIR-V Backend (PR #91736)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 10 06:07:03 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-spir-v
Author: Vyacheslav Levytskyy (VyacheslavLevytskyy)
<details>
<summary>Changes</summary>
This PR is to introduce support for 'spirv.Decorations' metadata node in SPIR-V Backend.
See also https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/main/docs/SPIRVRepresentationInLLVM.rst that describes `spirv.Decorations` as an important part of SPIRV-friendly LLVM IR.
---
Full diff: https://github.com/llvm/llvm-project/pull/91736.diff
4 Files Affected:
- (modified) llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp (+7)
- (modified) llvm/lib/Target/SPIRV/SPIRVUtils.cpp (+28)
- (modified) llvm/lib/Target/SPIRV/SPIRVUtils.h (+4)
- (added) llvm/test/CodeGen/SPIRV/spirv-decoration.ll (+19)
``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index cebe230d3e8ce..0a4e44e2dac70 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -691,6 +691,13 @@ Register SPIRVGlobalRegistry::buildGlobalVariable(
buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::BuiltIn,
{static_cast<uint32_t>(BuiltInId)});
+ // If it's a global variable with "spirv.Decorations" metadata node
+ // recognize it as a SPIR-V friendly LLVM IR and parse "spirv.Decorations"
+ // arguments.
+ MDNode *GVarMD = nullptr;
+ if (GVar && (GVarMD = GVar->getMetadata("spirv.Decorations")) != nullptr)
+ buildOpSpirvDecorations(Reg, MIRBuilder, GVarMD);
+
return Reg;
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 871b95a28068e..3f8613d2707b8 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -133,6 +133,34 @@ void buildOpDecorate(Register Reg, MachineInstr &I, const SPIRVInstrInfo &TII,
finishBuildOpDecorate(MIB, DecArgs, StrImm);
}
+void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder,
+ MDNode *GVarMD) {
+ for (unsigned I = 0, E = GVarMD->getNumOperands(); I != E; ++I) {
+ auto *OpMD = dyn_cast<MDNode>(GVarMD->getOperand(I));
+ if (!OpMD)
+ report_fatal_error("Invalid decoration");
+ if (OpMD->getNumOperands() == 0)
+ report_fatal_error("Expect operand(s) of the decoration");
+ ConstantInt *DecorationId =
+ mdconst::dyn_extract<ConstantInt>(OpMD->getOperand(0));
+ if (!DecorationId)
+ report_fatal_error("Expect SPIR-V <Decoration> operand to be the first "
+ "element of the decoration");
+ auto MIB = MIRBuilder.buildInstr(SPIRV::OpDecorate)
+ .addUse(Reg)
+ .addImm(static_cast<uint32_t>(DecorationId->getZExtValue()));
+ for (unsigned OpI = 1, OpE = OpMD->getNumOperands(); OpI != OpE; ++OpI) {
+ if (ConstantInt *OpV =
+ mdconst::dyn_extract<ConstantInt>(OpMD->getOperand(OpI)))
+ MIB.addImm(static_cast<uint32_t>(OpV->getZExtValue()));
+ else if (MDString *OpV = dyn_cast<MDString>(OpMD->getOperand(OpI)))
+ addStringImm(OpV->getString(), MIB);
+ else
+ report_fatal_error("Unexpected operand of the decoration");
+ }
+ }
+}
+
// TODO: maybe the following two functions should be handled in the subtarget
// to allow for different OpenCL vs Vulkan handling.
unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC) {
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 6a91b6e576f9c..fbdc9bf75dbe1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -59,6 +59,10 @@ void buildOpDecorate(Register Reg, MachineInstr &I, const SPIRVInstrInfo &TII,
const std::vector<uint32_t> &DecArgs,
StringRef StrImm = "");
+// Add an OpDecorate instruction by "spirv.Decorations" metadata node
+void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder,
+ MDNode *GVarMD);
+
// Convert a SPIR-V storage class to the corresponding LLVM IR address space.
unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC);
diff --git a/llvm/test/CodeGen/SPIRV/spirv-decoration.ll b/llvm/test/CodeGen/SPIRV/spirv-decoration.ll
new file mode 100644
index 0000000000000..618646de93183
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/spirv-decoration.ll
@@ -0,0 +1,19 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: OpName %[[#GV:]] "v"
+; CHECK-DAG: OpDecorate %[[#GV]] LinkageAttributes "v" Export
+; CHECK-DAG: OpDecorate %[[#GV]] Constant
+
+ at v = addrspace(1) global i32 0, !spirv.Decorations !0
+
+define spir_kernel void @foo() {
+entry:
+ %pv = load ptr addrspace(1), ptr addrspace(1) @v
+ store i32 3, ptr addrspace(1) %pv
+ ret void
+}
+
+!0 = !{!1, !2}
+!1 = !{i32 22}
+!2 = !{i32 41, !"v", i32 0}
``````````
</details>
https://github.com/llvm/llvm-project/pull/91736
More information about the llvm-commits
mailing list