[Mlir-commits] [mlir] [mlir][spirv] Add gpu printf op lowering to spirv.CL.printf op (PR #78510)

Dimple Prajapati llvmlistbot at llvm.org
Thu Sep 26 22:38:42 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));
----------------
drprajap wrote:

Done, moved this to helper function that is used to construct globalVar and SpecConstant. It is using std::string and Twine::str() now.

https://github.com/llvm/llvm-project/pull/78510


More information about the Mlir-commits mailing list