@@ -53,37 +53,12 @@ class FuncOpLowering : public OpConversionPattern<cxx::FuncOp> {
53
53
-> LogicalResult override {
54
54
auto typeConverter = getTypeConverter ();
55
55
56
- auto funcType = op.getFunctionType ();
57
-
58
- SmallVector<Type> argumentTypes;
59
- for (auto argType : funcType.getInputs ()) {
60
- auto convertedType = typeConverter->convertType (argType);
61
- if (!convertedType) {
62
- return rewriter.notifyMatchFailure (
63
- op, " failed to convert function argument type" );
64
- }
65
- argumentTypes.push_back (convertedType);
56
+ if (failed (convertFunctionTyype (op, rewriter))) {
57
+ return rewriter.notifyMatchFailure (op, " failed to convert function type" );
66
58
}
67
59
68
- SmallVector<Type> resultTypes;
69
- for (auto resultType : funcType.getResults ()) {
70
- auto convertedType = typeConverter->convertType (resultType);
71
- if (!convertedType) {
72
- return rewriter.notifyMatchFailure (
73
- op, " failed to convert function result type" );
74
- }
75
-
76
- resultTypes.push_back (convertedType);
77
- }
78
-
79
- const auto returnType = resultTypes.empty ()
80
- ? LLVM::LLVMVoidType::get (getContext ())
81
- : resultTypes.front ();
82
-
83
- const auto isVarArg = funcType.getVariadic ();
84
-
85
- auto llvmFuncType =
86
- LLVM::LLVMFunctionType::get (returnType, argumentTypes, isVarArg);
60
+ auto funcType = op.getFunctionType ();
61
+ auto llvmFuncType = typeConverter->convertType (funcType);
87
62
88
63
auto func = rewriter.create <LLVM::LLVMFuncOp>(op.getLoc (), op.getSymName (),
89
64
llvmFuncType);
@@ -98,6 +73,29 @@ class FuncOpLowering : public OpConversionPattern<cxx::FuncOp> {
98
73
99
74
return success ();
100
75
}
76
+
77
+ auto convertFunctionTyype (cxx::FuncOp funcOp,
78
+ ConversionPatternRewriter &rewriter) const
79
+ -> LogicalResult {
80
+ auto type = funcOp.getFunctionType ();
81
+ const auto &typeConverter = *getTypeConverter ();
82
+
83
+ TypeConverter::SignatureConversion result (type.getInputs ().size ());
84
+ SmallVector<Type, 1 > newResults;
85
+ if (failed (typeConverter.convertSignatureArgs (type.getInputs (), result)) ||
86
+ failed (typeConverter.convertTypes (type.getResults (), newResults)) ||
87
+ failed (rewriter.convertRegionTypes (&funcOp.getFunctionBody (),
88
+ typeConverter, &result)))
89
+ return failure ();
90
+
91
+ auto newType = cxx::FunctionType::get (rewriter.getContext (),
92
+ result.getConvertedTypes (),
93
+ newResults, type.getVariadic ());
94
+
95
+ rewriter.modifyOpInPlace (funcOp, [&] { funcOp.setType (newType); });
96
+
97
+ return success ();
98
+ }
101
99
};
102
100
103
101
class GlobalOpLowering : public OpConversionPattern <cxx::GlobalOp> {
@@ -334,6 +332,45 @@ class SubscriptOpLowering : public OpConversionPattern<cxx::SubscriptOp> {
334
332
const DataLayout &dataLayout_;
335
333
};
336
334
335
+ class MemberOpLowering : public OpConversionPattern <cxx::MemberOp> {
336
+ public:
337
+ MemberOpLowering (const TypeConverter &typeConverter,
338
+ const DataLayout &dataLayout, MLIRContext *context,
339
+ PatternBenefit benefit = 1 )
340
+ : OpConversionPattern<cxx::MemberOp>(typeConverter, context, benefit),
341
+ dataLayout_ (dataLayout) {}
342
+
343
+ auto matchAndRewrite (cxx::MemberOp op, OpAdaptor adaptor,
344
+ ConversionPatternRewriter &rewriter) const
345
+ -> LogicalResult override {
346
+ auto typeConverter = getTypeConverter ();
347
+ auto context = getContext ();
348
+
349
+ auto pointerType = cast<cxx::PointerType>(op.getBase ().getType ());
350
+ auto classType = dyn_cast<cxx::ClassType>(pointerType.getElementType ());
351
+
352
+ auto resultType = typeConverter->convertType (op.getResult ().getType ());
353
+ if (!resultType) {
354
+ return rewriter.notifyMatchFailure (
355
+ op, " failed to convert member result type" );
356
+ }
357
+
358
+ auto elementType = typeConverter->convertType (classType);
359
+
360
+ SmallVector<LLVM::GEPArg> indices;
361
+ indices.push_back (0 );
362
+ indices.push_back (adaptor.getMemberIndex ());
363
+
364
+ rewriter.replaceOpWithNewOp <LLVM::GEPOp>(op, resultType, elementType,
365
+ adaptor.getBase (), indices);
366
+
367
+ return success ();
368
+ }
369
+
370
+ private:
371
+ const DataLayout &dataLayout_;
372
+ };
373
+
337
374
class BoolConstantOpLowering : public OpConversionPattern <cxx::BoolConstantOp> {
338
375
public:
339
376
using OpConversionPattern::OpConversionPattern;
@@ -1329,7 +1366,8 @@ void CxxToLLVMLoweringPass::runOnOperation() {
1329
1366
DataLayout dataLayout{module };
1330
1367
1331
1368
patterns.insert <AllocaOpLowering, LoadOpLowering, StoreOpLowering,
1332
- SubscriptOpLowering>(typeConverter, dataLayout, context);
1369
+ SubscriptOpLowering, MemberOpLowering>(typeConverter,
1370
+ dataLayout, context);
1333
1371
1334
1372
// cast operations
1335
1373
patterns.insert <IntToBoolOpLowering, BoolToIntOpLowering,
0 commit comments