[Mlir-commits] [mlir] [mlir][spirv] Add gpu printf op lowering to spirv.CL.printf op (PR #78510)
Jakub Kuderski
llvmlistbot at llvm.org
Wed Sep 18 06:50:46 PDT 2024
================
@@ -597,6 +606,120 @@ class GPUSubgroupReduceConversion final
}
};
+/// Pattern to convert a gpu.printf op into a SPIR-V CLPrintf op.
+
+LogicalResult GPUPrintfConversion::matchAndRewrite(
+ gpu::PrintfOp gpuPrintfOp, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const {
+
+ Location loc = gpuPrintfOp.getLoc();
+
+ auto moduleOp = gpuPrintfOp->getParentOfType<spirv::ModuleOp>();
+
+ if (!moduleOp) {
+ return success();
+ }
+
+ const char formatStringPrefix[] = "printfMsg";
+ unsigned stringNumber = 0;
+ SmallString<16> globalVarName;
+ spirv::GlobalVariableOp globalVar;
+
+ // SPIR-V global variable is used to initialize printf
+ // format string value, if there are multiple printf messages,
+ // each global var needs to be created with a unique name.
+ // like printfMsg0, printfMsg1, ...
+ // Formulate unique global variable name after
+ // searching in the module for existing global variable names.
+ // This is to avoid name collision with existing global variables.
+ do {
+ globalVarName.clear();
+ (formatStringPrefix + llvm::Twine(stringNumber++))
+ .toStringRef(globalVarName);
+ } while (moduleOp.lookupSymbol(globalVarName));
----------------
kuhar wrote:
Could you make this into a helper function? This one is already a bit lengthy. Also, I'd use plain `std::string` here as the small size is less than what I'd expect the regular SSO length to be (20-ish characters). You can use `Twine::str()` to produce it.
https://github.com/llvm/llvm-project/pull/78510
More information about the Mlir-commits
mailing list