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/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..0af89bce 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 @@ -253,21 +256,22 @@ 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"); } - 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 { @@ -405,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; @@ -1189,13 +1258,15 @@ void CxxToLLVMLoweringPass::runOnOperation() { typeConverter, context); // memory operations + DataLayout dataLayout{module}; + patterns.insert(typeConverter, context); + SubscriptOpLowering>(typeConverter, dataLayout, context); // cast operations - patterns - .insert( - typeConverter, context); + patterns.insert( + typeConverter, context); // constant operations patterns.insert(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_)); }