From a0231999ebb33af09099b25267cb2e75d5469361 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Mon, 11 Aug 2025 20:11:08 +0200 Subject: [PATCH 1/2] Fix triple for macos and wasm32 data layout Signed-off-by: Roberto Raggi --- src/mlir/cxx/mlir/codegen_units.cc | 5 + src/mlir/cxx/mlir/cxx_dialect_conversions.cc | 103 +++++++++++++------ src/parser/cxx/macos_toolchain.cc | 4 +- 3 files changed, 81 insertions(+), 31 deletions(-) diff --git a/src/mlir/cxx/mlir/codegen_units.cc b/src/mlir/cxx/mlir/codegen_units.cc index 06955445..9ecac161 100644 --- a/src/mlir/cxx/mlir/codegen_units.cc +++ b/src/mlir/cxx/mlir/codegen_units.cc @@ -108,6 +108,11 @@ auto Codegen::UnitVisitor::operator()(TranslationUnitAST* ast) -> UnitResult { module->setAttr("cxx.triple", mlir::StringAttr::get(module->getContext(), memoryLayout->triple())); + module->setAttr( + "cxx.data-layout", + mlir::StringAttr::get(module->getContext(), + llvmDataLayout.getStringRepresentation())); + std::swap(gen.module_, module); ForEachExternalDefinition forEachExternalDefinition; diff --git a/src/mlir/cxx/mlir/cxx_dialect_conversions.cc b/src/mlir/cxx/mlir/cxx_dialect_conversions.cc index b34b71c0..59614f9b 100644 --- a/src/mlir/cxx/mlir/cxx_dialect_conversions.cc +++ b/src/mlir/cxx/mlir/cxx_dialect_conversions.cc @@ -147,7 +147,11 @@ class CallOpLowering : public OpConversionPattern { class AllocaOpLowering : public OpConversionPattern { public: - using OpConversionPattern::OpConversionPattern; + AllocaOpLowering(const TypeConverter &typeConverter, + const DataLayout &dataLayout, MLIRContext *context, + PatternBenefit benefit = 1) + : OpConversionPattern(typeConverter, context, benefit), + dataLayout_(dataLayout) {} auto matchAndRewrite(cxx::AllocaOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const @@ -170,17 +174,25 @@ class AllocaOpLowering : public OpConversionPattern { } auto size = rewriter.create( - op.getLoc(), rewriter.getI64Type(), rewriter.getIndexAttr(1)); + op.getLoc(), typeConverter->convertType(rewriter.getIndexType()), + rewriter.getIntegerAttr(rewriter.getIndexType(), 1)); auto x = rewriter.replaceOpWithNewOp(op, resultType, elementType, size); return success(); } + + private: + const DataLayout &dataLayout_; }; class LoadOpLowering : public OpConversionPattern { public: - using OpConversionPattern::OpConversionPattern; + LoadOpLowering(const TypeConverter &typeConverter, + const DataLayout &dataLayout, MLIRContext *context, + PatternBenefit benefit = 1) + : OpConversionPattern(typeConverter, context, benefit), + dataLayout_(dataLayout) {} auto matchAndRewrite(cxx::LoadOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const @@ -195,11 +207,18 @@ class LoadOpLowering : public OpConversionPattern { return success(); } + + private: + const DataLayout &dataLayout_; }; class StoreOpLowering : public OpConversionPattern { public: - using OpConversionPattern::OpConversionPattern; + StoreOpLowering(const TypeConverter &typeConverter, + const DataLayout &dataLayout, MLIRContext *context, + PatternBenefit benefit = 1) + : OpConversionPattern(typeConverter, context, benefit), + dataLayout_(dataLayout) {} auto matchAndRewrite(cxx::StoreOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const @@ -218,34 +237,18 @@ class StoreOpLowering : public OpConversionPattern { return success(); } -}; - -class BoolConstantOpLowering : public OpConversionPattern { - public: - using OpConversionPattern::OpConversionPattern; - auto matchAndRewrite(cxx::BoolConstantOp op, OpAdaptor adaptor, - ConversionPatternRewriter &rewriter) const - -> LogicalResult override { - auto typeConverter = getTypeConverter(); - auto context = getContext(); - - auto resultType = typeConverter->convertType(op.getType()); - if (!resultType) { - return rewriter.notifyMatchFailure( - op, "failed to convert boolean constant type"); - } - - rewriter.replaceOpWithNewOp(op, resultType, - adaptor.getValue()); - - return success(); - } + private: + const DataLayout &dataLayout_; }; class SubscriptOpLowering : public OpConversionPattern { public: - using OpConversionPattern::OpConversionPattern; + SubscriptOpLowering(const TypeConverter &typeConverter, + const DataLayout &dataLayout, MLIRContext *context, + PatternBenefit benefit = 1) + : OpConversionPattern(typeConverter, context, benefit), + dataLayout_(dataLayout) {} auto matchAndRewrite(cxx::SubscriptOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const @@ -266,8 +269,9 @@ class SubscriptOpLowering : public OpConversionPattern { op, "expected base type of subscript to be an array type"); } - SmallVector indices; + SmallVector indices; + indices.push_back(0); indices.push_back(adaptor.getIndex()); auto resultType = LLVM::LLVMPointerType::get(context); @@ -278,6 +282,32 @@ class SubscriptOpLowering : public OpConversionPattern { return success(); } + + private: + const DataLayout &dataLayout_; +}; + +class BoolConstantOpLowering : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + auto matchAndRewrite(cxx::BoolConstantOp op, OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const + -> LogicalResult override { + auto typeConverter = getTypeConverter(); + auto context = getContext(); + + auto resultType = typeConverter->convertType(op.getType()); + if (!resultType) { + return rewriter.notifyMatchFailure( + op, "failed to convert boolean constant type"); + } + + rewriter.replaceOpWithNewOp(op, resultType, + adaptor.getValue()); + + return success(); + } }; class IntConstantOpLowering : public OpConversionPattern { @@ -1189,8 +1219,10 @@ void CxxToLLVMLoweringPass::runOnOperation() { typeConverter, context); // memory operations + DataLayout dataLayout{module}; + patterns.insert(typeConverter, context); + SubscriptOpLowering>(typeConverter, dataLayout, context); // cast operations patterns @@ -1236,7 +1268,20 @@ void CxxToLLVMLoweringPass::runOnOperation() { if (failed(applyPartialConversion(module, target, std::move(patterns)))) { signalPassFailure(); + return; } + + auto targetTriple = + mlir::cast(module->getAttr("cxx.triple")); + + module->setAttr(LLVM::LLVMDialect::getTargetTripleAttrName(), + mlir::StringAttr::get(context, targetTriple.str())); + + auto dataLayoutDescr = + mlir::cast(module->getAttr("cxx.data-layout")); + + module->setAttr(LLVM::LLVMDialect::getDataLayoutAttrName(), + mlir::StringAttr::get(context, dataLayoutDescr.str())); } } // namespace mlir diff --git a/src/parser/cxx/macos_toolchain.cc b/src/parser/cxx/macos_toolchain.cc index f3f7bdc2..4ee97e7b 100644 --- a/src/parser/cxx/macos_toolchain.cc +++ b/src/parser/cxx/macos_toolchain.cc @@ -42,10 +42,10 @@ MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor, std::string arch) if (arch_ == "aarch64") { memoryLayout()->setSizeOfLongDouble(8); - memoryLayout()->setTriple("aarch64-darwin"); + memoryLayout()->setTriple("arm64-apple-macosx15.0.0"); } else if (arch_ == "x86_64") { memoryLayout()->setSizeOfLongDouble(16); - memoryLayout()->setTriple("x86_64-apple-darwin24.6.0"); + memoryLayout()->setTriple("x86_64-apple-macosx15.0.0"); } else { cxx_runtime_error(std::format("Unsupported architecture: {}", arch_)); } From b8405726ccf850c80dd4dc217cf50cde9973e0ec Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Mon, 11 Aug 2025 21:07:13 +0200 Subject: [PATCH 2/2] Add ArrayToPointerOp and its conversion pattern Signed-off-by: Roberto Raggi --- src/mlir/cxx/mlir/CxxOps.td | 6 +++ src/mlir/cxx/mlir/codegen_expressions.cc | 11 +++++ src/mlir/cxx/mlir/cxx_dialect_conversions.cc | 49 ++++++++++++++++++-- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/mlir/cxx/mlir/CxxOps.td b/src/mlir/cxx/mlir/CxxOps.td index bf704d59..0d317992 100644 --- a/src/mlir/cxx/mlir/CxxOps.td +++ b/src/mlir/cxx/mlir/CxxOps.td @@ -202,6 +202,12 @@ def Cxx_IntegralCastOp : Cxx_Op<"integral_cast"> { let results = (outs Cxx_IntegerType:$result); } +def Cxx_ArrayToPointerOp : Cxx_Op<"array_to_pointer"> { + let arguments = (ins Cxx_PointerType:$value); + + let results = (outs Cxx_PointerType:$result); +} + def Cxx_NotOp : Cxx_Op<"not"> { let arguments = (ins AnyType:$value); diff --git a/src/mlir/cxx/mlir/codegen_expressions.cc b/src/mlir/cxx/mlir/codegen_expressions.cc index 9e0039ec..6f782b8d 100644 --- a/src/mlir/cxx/mlir/codegen_expressions.cc +++ b/src/mlir/cxx/mlir/codegen_expressions.cc @@ -1013,6 +1013,17 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast) break; } + case ImplicitCastKind::kArrayToPointerConversion: { + // generate an array to pointer conversion + auto expressionResult = gen.expression(ast->expression); + auto resultType = gen.convertType(ast->type); + + auto op = gen.builder_.create( + loc, resultType, expressionResult.value); + + return {op}; + } + default: break; diff --git a/src/mlir/cxx/mlir/cxx_dialect_conversions.cc b/src/mlir/cxx/mlir/cxx_dialect_conversions.cc index 59614f9b..0af89bce 100644 --- a/src/mlir/cxx/mlir/cxx_dialect_conversions.cc +++ b/src/mlir/cxx/mlir/cxx_dialect_conversions.cc @@ -256,14 +256,14 @@ class SubscriptOpLowering : public OpConversionPattern { auto typeConverter = getTypeConverter(); auto context = getContext(); - auto ptrType = dyn_cast_or_null(op.getBase().getType()); + auto ptrType = dyn_cast(op.getBase().getType()); if (!ptrType) { return rewriter.notifyMatchFailure( op, "failed to convert subscript operation type"); } - auto arrayType = dyn_cast_or_null(ptrType.getElementType()); + auto arrayType = dyn_cast(ptrType.getElementType()); if (!arrayType) { return rewriter.notifyMatchFailure( op, "expected base type of subscript to be an array type"); @@ -435,6 +435,45 @@ class IntToBoolOpLowering : public OpConversionPattern { } }; +class ArrayToPointerOpLowering + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + auto matchAndRewrite(cxx::ArrayToPointerOp op, OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const + -> LogicalResult override { + auto typeConverter = getTypeConverter(); + auto context = getContext(); + + auto ptrType = dyn_cast(op.getValue().getType()); + + if (!ptrType) { + return rewriter.notifyMatchFailure( + op, "failed to convert subscript operation type"); + } + + auto arrayType = dyn_cast(ptrType.getElementType()); + if (!arrayType) { + return rewriter.notifyMatchFailure( + op, "expected base type of subscript to be an array type"); + } + + SmallVector indices; + + indices.push_back(0); + indices.push_back(0); + + auto resultType = LLVM::LLVMPointerType::get(context); + auto elementType = typeConverter->convertType(ptrType.getElementType()); + + rewriter.replaceOpWithNewOp(op, resultType, elementType, + adaptor.getValue(), indices); + + return success(); + } +}; + class BoolToIntOpLowering : public OpConversionPattern { public: using OpConversionPattern::OpConversionPattern; @@ -1225,9 +1264,9 @@ void CxxToLLVMLoweringPass::runOnOperation() { SubscriptOpLowering>(typeConverter, dataLayout, context); // cast operations - patterns - .insert( - typeConverter, context); + patterns.insert( + typeConverter, context); // constant operations patterns.insert