[PATCH] D77364: [mlir][Linalg] Employ finer-grained control of C interface emission

Nicolas Vasilache via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 2 21:09:11 PDT 2020


nicolasvasilache created this revision.
nicolasvasilache added reviewers: ftynse, aartbik.
Herald added subscribers: llvm-commits, grosul1, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.

Linalg makes it possible to interface codegen with externally precompiled HPC libraries. The mechanism to allow such interop uses a normalized ABI and the emission of C interface wrappers.

The mechanism controlling these C interface emission is too aggressive and makes it very easy to obtained undefined symbols for external function (e.g. the ones coming from libm).

This revision uses the newly introduced llvm.emit_c_interface function attribute which allows controlling this behavior at a function granularity. As a consequence LinalgToLLVM does not need to activate the C wrapper emission when adding the StdToLLVM patterns.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77364

Files:
  mlir/lib/Conversion/LinalgToLLVM/LinalgToLLVM.cpp
  mlir/test/mlir-cpu-runner/unranked_memref.mlir
  mlir/test/mlir-cpu-runner/utils.mlir


Index: mlir/test/mlir-cpu-runner/utils.mlir
===================================================================
--- mlir/test/mlir-cpu-runner/utils.mlir
+++ mlir/test/mlir-cpu-runner/utils.mlir
@@ -50,7 +50,7 @@
 //    PRINT-3D-NEXT: 2,    2,    4,    2,    2
 //    PRINT-3D-NEXT: 2,    2,    2,    2,    2
 
-func @print_memref_f32(memref<*xf32>)
+func @print_memref_f32(memref<*xf32>) attributes { llvm.emit_c_interface }
 
 !vector_type_C = type vector<4x4xf32>
 !matrix_type_CC = type memref<1x1x!vector_type_C>
@@ -71,4 +71,4 @@
 // PRINT-VECTOR-SPLAT-2D: Memref base@ = {{.*}} rank = 2 offset = 0 sizes = [1, 1] strides = [1, 1] data =
 // PRINT-VECTOR-SPLAT-2D-NEXT: [((10, 10, 10, 10),   (10, 10, 10, 10),   (10, 10, 10, 10),   (10, 10, 10, 10))]
 
-func @print_memref_vector_4x4xf32(memref<?x?x!vector_type_C>)
+func @print_memref_vector_4x4xf32(memref<?x?x!vector_type_C>) attributes { llvm.emit_c_interface }
Index: mlir/test/mlir-cpu-runner/unranked_memref.mlir
===================================================================
--- mlir/test/mlir-cpu-runner/unranked_memref.mlir
+++ mlir/test/mlir-cpu-runner/unranked_memref.mlir
@@ -55,5 +55,5 @@
     return
 }
 
-func @print_memref_i8(memref<*xi8>)
-func @print_memref_f32(memref<*xf32>)
+func @print_memref_i8(memref<*xi8>) attributes { llvm.emit_c_interface }
+func @print_memref_f32(memref<*xf32>) attributes { llvm.emit_c_interface }
Index: mlir/lib/Conversion/LinalgToLLVM/LinalgToLLVM.cpp
===================================================================
--- mlir/lib/Conversion/LinalgToLLVM/LinalgToLLVM.cpp
+++ mlir/lib/Conversion/LinalgToLLVM/LinalgToLLVM.cpp
@@ -400,8 +400,10 @@
   // Insert before module terminator.
   rewriter.setInsertionPoint(module.getBody(),
                              std::prev(module.getBody()->end()));
-  rewriter.create<FuncOp>(op->getLoc(), fnNameAttr.getValue(), libFnType,
-                          ArrayRef<NamedAttribute>{});
+  FuncOp funcOp =
+      rewriter.create<FuncOp>(op->getLoc(), fnNameAttr.getValue(), libFnType,
+                              ArrayRef<NamedAttribute>{});
+  funcOp.setAttr("llvm.emit_c_interface", UnitAttr::get(op->getContext()));
   return fnNameAttr;
 }
 
@@ -573,8 +575,7 @@
   LLVMTypeConverter converter(&getContext());
   populateAffineToStdConversionPatterns(patterns, &getContext());
   populateLoopToStdConversionPatterns(patterns, &getContext());
-  populateStdToLLVMConversionPatterns(converter, patterns, /*useAlloca=*/false,
-                                      /*emitCWrappers=*/true);
+  populateStdToLLVMConversionPatterns(converter, patterns);
   populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
   populateVectorToLLVMConversionPatterns(converter, patterns);
   populateLinalgToStandardConversionPatterns(patterns, &getContext());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77364.254698.patch
Type: text/x-patch
Size: 2818 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200403/9f2cedba/attachment-0001.bin>


More information about the llvm-commits mailing list