@@ -646,7 +646,7 @@ Examples:
646
646
647
647
```mlir
648
648
649
- func.func @qux(%arg0: memref<?x?xf32>)
649
+ func.func @qux(%arg0: memref<?x?xf32>) attributes {llvm.emit_c_interface}
650
650
651
651
// Gets converted into the following
652
652
// (using type alias for brevity):
@@ -683,8 +683,18 @@ llvm.func @qux(%arg0: !llvm.ptr, %arg1: !llvm.ptr,
683
683
llvm.func @_mlir_ciface_qux(!llvm.ptr)
684
684
```
685
685
686
+
687
+ ``` cpp
688
+ // The C function implementation for the interface function.
689
+ extern "C" {
690
+ void _mlir_ciface_qux (MemRefDescriptor<float, 2> * input) {
691
+ // detailed impl
692
+ }
693
+ }
694
+ ```
695
+
686
696
```mlir
687
- func.func @foo(%arg0: memref<?x?xf32>) {
697
+ func.func @foo(%arg0: memref<?x?xf32>) attributes {llvm.emit_c_interface} {
688
698
return
689
699
}
690
700
@@ -719,8 +729,15 @@ llvm.func @_mlir_ciface_foo(%arg0: !llvm.ptr) {
719
729
}
720
730
```
721
731
732
+ ``` cpp
733
+ // The C function signature for the interface function.
734
+ extern "C" {
735
+ void _mlir_ciface_foo (MemRefDescriptor<float, 2> * input);
736
+ }
737
+ ```
738
+
722
739
```mlir
723
- func.func @foo(%arg0: memref<?x?xf32>) -> memref<?x?xf32> {
740
+ func.func @foo(%arg0: memref<?x?xf32>) -> memref<?x?xf32> attributes {llvm.emit_c_interface} {
724
741
return %arg0 : memref<?x?xf32>
725
742
}
726
743
@@ -744,6 +761,7 @@ llvm.func @foo(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64,
744
761
}
745
762
746
763
// Interface function callable from C.
764
+ // NOTE: the returned memref becomes the first argument
747
765
llvm.func @_mlir_ciface_foo(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
748
766
%0 = llvm.load %arg1 : !llvm.ptr
749
767
%1 = llvm.extractvalue %0[0] : !llvm.memref_2d
@@ -760,6 +778,14 @@ llvm.func @_mlir_ciface_foo(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
760
778
}
761
779
```
762
780
781
+ ``` cpp
782
+ // The C function signature for the interface function.
783
+ extern "C" {
784
+ void _mlir_ciface_foo (MemRefDescriptor<float, 2> * output,
785
+ MemRefDescriptor<float, 2> * input);
786
+ }
787
+ ```
788
+
763
789
Rationale: Introducing auxiliary functions for C-compatible interfaces is
764
790
preferred to modifying the calling convention since it will minimize the effect
765
791
of C compatibility on intra-module calls or calls between MLIR-generated
0 commit comments