Skip to content

Commit 24f81e4

Browse files
committed
add uncheckedGetIntrinsicSignature and mlirLLVMFunctionTypeGetReturnType
1 parent 4a32519 commit 24f81e4

File tree

7 files changed

+88
-4
lines changed

7 files changed

+88
-4
lines changed

mlir/include/mlir-c/Dialect/LLVM.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ MLIR_CAPI_EXPORTED intptr_t mlirLLVMFunctionTypeGetNumInputs(MlirType type);
5252
MLIR_CAPI_EXPORTED MlirType mlirLLVMFunctionTypeGetInput(MlirType type,
5353
intptr_t pos);
5454

55+
/// Returns the return type of the function type.
56+
MLIR_CAPI_EXPORTED MlirType mlirLLVMFunctionTypeGetReturnType(MlirType type);
57+
5558
/// Returns `true` if the type is an LLVM dialect struct type.
5659
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type);
5760

mlir/include/mlir-c/Target/LLVMIR.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ mlirTypeToLLVMIRTranslatorDestroy(MlirTypeToLLVMIRTranslator translator);
7474
MLIR_CAPI_EXPORTED LLVMTypeRef mlirTypeToLLVMIRTranslatorTranslateType(
7575
MlirTypeToLLVMIRTranslator translator, MlirType mlirType);
7676

77+
/// Attempts to look up the LLVM intrinsic matching `id` with parameter types
78+
/// `mlirParamTypes`. Note, since no return type is required, no check is
79+
/// performed to verify the found intrinsic.
80+
MLIR_CAPI_EXPORTED LLVMTypeRef
81+
mlirTypeToLLVMIRTranslatorUncheckedGetIntrinsicSignature(
82+
MlirTypeToLLVMIRTranslator translator, unsigned id,
83+
MlirType *mlirParamTypes, unsigned numParams);
84+
7785
#ifdef __cplusplus
7886
}
7987
#endif

mlir/include/mlir/Target/LLVMIR/TypeToLLVM.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
#ifndef MLIR_TARGET_LLVMIR_TYPETOLLVM_H
1515
#define MLIR_TARGET_LLVMIR_TYPETOLLVM_H
1616

17+
#include "llvm/ADT/ArrayRef.h"
18+
1719
#include <memory>
1820

1921
namespace llvm {
22+
class FunctionType;
2023
class DataLayout;
2124
class LLVMContext;
2225
class Type;
@@ -50,6 +53,12 @@ class TypeToLLVMIRTranslator {
5053
/// Translates the given MLIR LLVM dialect type to LLVM IR.
5154
llvm::Type *translateType(Type type);
5255

56+
/// Attempts to look up the LLVM intrinsic matching `id` with parameter types
57+
/// `types`. Note, since no return type is required, no check is performed to
58+
/// verify the found intrinsic.
59+
llvm::FunctionType *
60+
uncheckedGetIntrinsicSignature(unsigned id, llvm::ArrayRef<Type> types);
61+
5362
private:
5463
/// Private implementation.
5564
std::unique_ptr<detail::TypeToLLVMIRTranslatorImpl> impl;

mlir/lib/CAPI/Dialect/LLVM.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ MlirType mlirLLVMFunctionTypeGetInput(MlirType type, intptr_t pos) {
6565
.getParamType(static_cast<unsigned>(pos)));
6666
}
6767

68+
MlirType mlirLLVMFunctionTypeGetReturnType(MlirType type) {
69+
return wrap(llvm::cast<LLVM::LLVMFunctionType>(unwrap(type)).getReturnType());
70+
}
71+
6872
bool mlirTypeIsALLVMStructType(MlirType type) {
6973
return isa<LLVM::LLVMStructType>(unwrap(type));
7074
}

mlir/lib/CAPI/Target/LLVMIR.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "mlir-c/Target/LLVMIR.h"
1111

12+
#include "llvm/IR/Intrinsics.h"
1213
#include "llvm/IR/LLVMContext.h"
1314
#include "llvm/IR/Module.h"
1415
#include "llvm/IR/Type.h"
@@ -77,3 +78,15 @@ mlirTypeToLLVMIRTranslatorTranslateType(MlirTypeToLLVMIRTranslator translator,
7778
llvm::Type *type = translator_->translateType(unwrap(mlirType));
7879
return llvm::wrap(type);
7980
}
81+
82+
LLVMTypeRef mlirTypeToLLVMIRTranslatorUncheckedGetIntrinsicSignature(
83+
MlirTypeToLLVMIRTranslator translator, unsigned id,
84+
MlirType *mlirParamTypes, unsigned numParams) {
85+
SmallVector<mlir::Type, 4> paramTypes;
86+
for (unsigned i = 0; i < numParams; i++)
87+
paramTypes.emplace_back(unwrap(mlirParamTypes[i]));
88+
LLVM::TypeToLLVMIRTranslator *translator_ = unwrap(translator);
89+
llvm::Type *functionType =
90+
translator_->uncheckedGetIntrinsicSignature(id, paramTypes);
91+
return llvm::wrap(functionType);
92+
}

mlir/lib/Target/LLVMIR/TypeToLLVM.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ADT/TypeSwitch.h"
1515
#include "llvm/IR/DataLayout.h"
1616
#include "llvm/IR/DerivedTypes.h"
17+
#include "llvm/IR/Intrinsics.h"
1718
#include "llvm/IR/Type.h"
1819

1920
using namespace mlir;
@@ -84,6 +85,15 @@ class TypeToLLVMIRTranslatorImpl {
8485
return translated;
8586
}
8687

88+
llvm::FunctionType *
89+
uncheckedGetIntrinsicSignature(unsigned id, llvm::ArrayRef<Type> types) {
90+
SmallVector<llvm::Type *, 8> paramTypes;
91+
if (llvm::Intrinsic::isOverloaded(id)) {
92+
translateTypes(types, paramTypes);
93+
}
94+
return llvm::Intrinsic::getType(context, id, paramTypes);
95+
}
96+
8797
private:
8898
/// Translates the given array type.
8999
llvm::Type *translate(LLVM::LLVMArrayType type) {
@@ -193,6 +203,12 @@ llvm::Type *LLVM::TypeToLLVMIRTranslator::translateType(Type type) {
193203
return impl->translateType(type);
194204
}
195205

206+
llvm::FunctionType *
207+
LLVM::TypeToLLVMIRTranslator::uncheckedGetIntrinsicSignature(
208+
unsigned id, llvm::ArrayRef<Type> types) {
209+
return impl->uncheckedGetIntrinsicSignature(id, types);
210+
}
211+
196212
unsigned LLVM::TypeToLLVMIRTranslator::getPreferredAlignment(
197213
Type type, const llvm::DataLayout &layout) {
198214
return layout.getPrefTypeAlign(translateType(type)).value();

mlir/test/CAPI/translation.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ static void testToLLVMIR(MlirContext ctx) {
5858
LLVMContextDispose(llvmCtx);
5959
}
6060

61-
// CHECK-LABEL: testTypeFromLLVMIRTranslator
62-
static void testTypeFromLLVMIRTranslator(MlirContext ctx) {
63-
fprintf(stderr, "testTypeFromLLVMIRTranslator\n");
61+
// CHECK-LABEL: testTypeToFromLLVMIRTranslator
62+
static void testTypeToFromLLVMIRTranslator(MlirContext ctx) {
63+
fprintf(stderr, "testTypeToFromLLVMIRTranslator\n");
6464
LLVMContextRef llvmCtx = LLVMContextCreate();
6565

6666
LLVMTypeRef llvmTy = LLVMInt32TypeInContext(llvmCtx);
@@ -77,6 +77,37 @@ static void testTypeFromLLVMIRTranslator(MlirContext ctx) {
7777
mlirTypeToLLVMIRTranslatorTranslateType(toLLVMTranslator, mlirTy);
7878
// CHECK: i32
7979
LLVMDumpType(llvmTy2);
80+
fprintf(stderr, "\n");
81+
82+
// check "not overloaded" path
83+
MlirType i32 = mlirIntegerTypeGet(ctx, 32);
84+
MlirType nonOverloadeTys[] = {i32};
85+
unsigned returnAddressIID = LLVMLookupIntrinsicID("llvm.returnaddress", 18);
86+
LLVMTypeRef returnAddressIntrinsicTy =
87+
mlirTypeToLLVMIRTranslatorUncheckedGetIntrinsicSignature(
88+
toLLVMTranslator, returnAddressIID, nonOverloadeTys, 1);
89+
// CHECK: ptr (i32)
90+
LLVMDumpType(returnAddressIntrinsicTy);
91+
fprintf(stderr, "\n");
92+
93+
// check "overloaded" path
94+
MlirType overloadeTys[] = {i32, i32};
95+
unsigned sMaxIID = LLVMLookupIntrinsicID("llvm.smax", 9);
96+
LLVMTypeRef llvmSMaxIntrinsicTy =
97+
mlirTypeToLLVMIRTranslatorUncheckedGetIntrinsicSignature(
98+
toLLVMTranslator, sMaxIID, overloadeTys, 2);
99+
// CHECK: i32 (i32, i32)
100+
LLVMDumpType(llvmSMaxIntrinsicTy);
101+
fprintf(stderr, "\n");
102+
103+
MlirType mlirSMaxIntrinsicTy = mlirTypeFromLLVMIRTranslatorTranslateType(
104+
fromLLVMTranslator, llvmSMaxIntrinsicTy);
105+
// CHECK: !llvm.func<i32 (i32, i32)>
106+
mlirTypeDump(mlirSMaxIntrinsicTy);
107+
108+
MlirType retType = mlirLLVMFunctionTypeGetReturnType(mlirSMaxIntrinsicTy);
109+
// CHECK: i32
110+
mlirTypeDump(retType);
80111

81112
mlirTypeFromLLVMIRTranslatorDestroy(fromLLVMTranslator);
82113
mlirTypeToLLVMIRTranslatorDestroy(toLLVMTranslator);
@@ -88,7 +119,7 @@ int main(void) {
88119
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
89120
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
90121
testToLLVMIR(ctx);
91-
testTypeFromLLVMIRTranslator(ctx);
122+
testTypeToFromLLVMIRTranslator(ctx);
92123
mlirContextDestroy(ctx);
93124
return 0;
94125
}

0 commit comments

Comments
 (0)