[Mlir-commits] [mlir] [MLIR][GPU-LLVM] Add in-pass signature update option for opencl kernels (PR #105664)

Victor Perez llvmlistbot at llvm.org
Tue Sep 17 00:44:11 PDT 2024


================
@@ -325,16 +359,45 @@ struct GPUToLLVMSPVConversionPass final
     LLVMTypeConverter converter(context, options);
     LLVMConversionTarget target(*context);
 
+    if (forceOpenclAddressSpaces) {
+      MemorySpaceToOpenCLMemorySpaceConverter converter;
+      AttrTypeReplacer replacer;
+      replacer.addReplacement([&converter](BaseMemRefType origType)
+                                  -> std::optional<BaseMemRefType> {
+        return converter.convertType<BaseMemRefType>(origType);
+      });
+
+      replacer.recursivelyReplaceElementsIn(getOperation(),
+                                            /*replaceAttrs=*/true,
+                                            /*replaceLocs=*/false,
+                                            /*replaceTypes=*/true);
+    }
+
     target.addIllegalOp<gpu::BarrierOp, gpu::BlockDimOp, gpu::BlockIdOp,
                         gpu::GPUFuncOp, gpu::GlobalIdOp, gpu::GridDimOp,
                         gpu::ReturnOp, gpu::ShuffleOp, gpu::ThreadIdOp>();
 
     populateGpuToLLVMSPVConversionPatterns(converter, patterns);
+    populateFuncToLLVMConversionPatterns(converter, patterns);
+    populateFinalizeMemRefToLLVMConversionPatterns(converter, patterns);
     populateGpuMemorySpaceAttributeConversions(converter);
 
     if (failed(applyPartialConversion(getOperation(), target,
                                       std::move(patterns))))
       signalPassFailure();
+
+    // `func.func`s are not handled by the lowering, so need a proper calling
+    // convention set separately.
+    getOperation().walk([](LLVM::LLVMFuncOp f) {
+      if (f.getCConv() == LLVM::CConv::C) {
+        f.setCConv(LLVM::CConv::SPIR_FUNC);
+      }
+    });
+    getOperation().walk([](LLVM::CallOp c) {
+      if (c.getCConv() == LLVM::CConv::C) {
+        c.setCConv(LLVM::CConv::SPIR_FUNC);
+      }
+    });
----------------
victor-eds wrote:

I'd prefer this pass to stick to its promise of "converting *GPU* ops to LLVM for SPIR-V backend ingestion"

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


More information about the Mlir-commits mailing list