Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ uint64_t RecordType::getElementOffset(const ::mlir::DataLayout &dataLayout,
// offset we are calculating.
const llvm::Align tyAlign = llvm::Align(
getPacked() ? 1 : dataLayout.getTypeABIAlignment(members[idx]));
offset = llvm::alignTo(offset, tyAlign);

offset = llvm::alignTo(offset, tyAlign);
return offset;
}

Expand Down Expand Up @@ -781,8 +781,8 @@ LongDoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
uint64_t
LongDoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying()).getABIAlignment(
dataLayout, params);
return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
.getABIAlignment(dataLayout, params);
}

//===----------------------------------------------------------------------===//
Expand Down
139 changes: 100 additions & 39 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "LowerToMLIRHelpers.h"
#include "mlir/Analysis/DataLayoutAnalysis.h"
#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
Expand All @@ -35,8 +36,10 @@
#include "mlir/IR/Operation.h"
#include "mlir/IR/Region.h"
#include "mlir/IR/TypeRange.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/ValueRange.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Support/LLVM.h"
Expand All @@ -48,19 +51,17 @@
#include "mlir/Transforms/DialectConversion.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h"
#include "clang/CIR/LowerToLLVM.h"
#include "clang/CIR/LowerToMLIR.h"
#include "clang/CIR/LoweringHelpers.h"
#include "clang/CIR/Passes.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/ErrorHandling.h"
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h"
#include "clang/CIR/LowerToLLVM.h"
#include "clang/CIR/Passes.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TimeProfiler.h"

using namespace cir;
Expand Down Expand Up @@ -288,17 +289,17 @@ class CIRAllocaOpLowering : public mlir::OpConversionPattern<cir::AllocaOp> {
matchAndRewrite(cir::AllocaOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {

mlir::Type mlirType =
convertTypeForMemory(*getTypeConverter(), adaptor.getAllocaType());
mlir::Type allocaType = adaptor.getAllocaType();
mlir::Type mlirType = convertTypeForMemory(*getTypeConverter(), allocaType);

// FIXME: Some types can not be converted yet (e.g. struct)
if (!mlirType)
return mlir::LogicalResult::failure();

auto memreftype = mlir::dyn_cast<mlir::MemRefType>(mlirType);
if (memreftype && mlir::isa<cir::ArrayType>(adaptor.getAllocaType())) {
// if the type is an array,
// we don't need to wrap with memref.
if (memreftype && (mlir::isa<cir::ArrayType>(allocaType) ||
mlir::isa<cir::RecordType>(allocaType))) {
// Arrays and structs are already memref. No need to wrap another one.
} else {
memreftype = mlir::MemRefType::get({}, mlirType);
}
Expand Down Expand Up @@ -946,8 +947,8 @@ class CIRScopeOpLowering : public mlir::OpConversionPattern<cir::ScopeOp> {
} else {
// For scopes with results, use scf.execute_region
SmallVector<mlir::Type> types;
if (mlir::failed(
getTypeConverter()->convertTypes(scopeOp->getResultTypes(), types)))
if (mlir::failed(getTypeConverter()->convertTypes(
scopeOp->getResultTypes(), types)))
return mlir::failure();
auto exec =
rewriter.create<mlir::scf::ExecuteRegionOp>(scopeOp.getLoc(), types);
Expand Down Expand Up @@ -1485,6 +1486,51 @@ class CIRPtrStrideOpLowering
}
};

class CIRGetMemberOpLowering
: public mlir::OpConversionPattern<cir::GetMemberOp> {
public:
CIRGetMemberOpLowering(mlir::TypeConverter &converter, mlir::MLIRContext *ctx,
const mlir::DataLayout &layout)
: OpConversionPattern(converter, ctx), layout(layout) {}

mlir::LogicalResult
matchAndRewrite(cir::GetMemberOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {

cir::PointerType ptrType = op.getAddr().getType();

auto structType = mlir::dyn_cast<cir::RecordType>(ptrType.getPointee());
if (!structType) {
return rewriter.notifyMatchFailure(
op, "expected RecordType as pointee of GetMemberOp base");
}

uint64_t byteOffset = structType.getElementOffset(layout, op.getIndex());
cir::PointerType fieldType = op.getResult().getType();
auto convertedType = getTypeConverter()->convertType(fieldType);
if (!convertedType) {
return rewriter.notifyMatchFailure(op, "failed to convert field type");
}

auto resultType = mlir::dyn_cast<mlir::MemRefType>(convertedType);
if (!resultType) {
return rewriter.notifyMatchFailure(
op, "expected MemRefType after type conversion");
}

mlir::Value offsetValue =
rewriter.create<mlir::arith::ConstantIndexOp>(op.getLoc(), byteOffset);

rewriter.replaceOpWithNewOp<mlir::memref::ViewOp>(
op, resultType, adaptor.getAddr(), offsetValue, mlir::ValueRange{});

return mlir::success();
}

private:
const mlir::DataLayout &layout;
};

class CIRUnreachableOpLowering
: public mlir::OpConversionPattern<cir::UnreachableOp> {
public:
Expand Down Expand Up @@ -1516,37 +1562,41 @@ class CIRTrapOpLowering : public mlir::OpConversionPattern<cir::TrapOp> {
};

void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
mlir::TypeConverter &converter) {
mlir::TypeConverter &converter,
mlir::DataLayout layout) {
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());

patterns
.add<CIRATanOpLowering, CIRCmpOpLowering, CIRCallOpLowering,
CIRUnaryOpLowering, CIRBinOpLowering, CIRLoadOpLowering,
CIRConstantOpLowering, CIRStoreOpLowering, CIRAllocaOpLowering,
CIRFuncOpLowering, CIRBrCondOpLowering,
CIRTernaryOpLowering, CIRYieldOpLowering, CIRCosOpLowering,
CIRGlobalOpLowering, CIRGetGlobalOpLowering, CIRCastOpLowering,
CIRPtrStrideOpLowering, CIRGetElementOpLowering, CIRSqrtOpLowering,
CIRCeilOpLowering, CIRExp2OpLowering, CIRExpOpLowering,
CIRFAbsOpLowering, CIRAbsOpLowering, CIRFloorOpLowering,
CIRLog10OpLowering, CIRLog2OpLowering, CIRLogOpLowering,
CIRRoundOpLowering, CIRSinOpLowering, CIRShiftOpLowering,
CIRBitClzOpLowering, CIRBitCtzOpLowering, CIRBitPopcountOpLowering,
CIRBitClrsbOpLowering, CIRBitFfsOpLowering, CIRBitParityOpLowering,
CIRIfOpLowering, CIRVectorCreateLowering, CIRVectorInsertLowering,
CIRVectorExtractLowering, CIRVectorCmpOpLowering, CIRACosOpLowering,
CIRASinOpLowering, CIRUnreachableOpLowering, CIRTanOpLowering,
CIRTrapOpLowering>(converter, patterns.getContext());
patterns.add<
CIRATanOpLowering, CIRCmpOpLowering, CIRCallOpLowering,
CIRUnaryOpLowering, CIRBinOpLowering, CIRLoadOpLowering,
CIRConstantOpLowering, CIRStoreOpLowering, CIRAllocaOpLowering,
CIRFuncOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
CIRGetGlobalOpLowering, CIRCastOpLowering, CIRPtrStrideOpLowering,
CIRGetElementOpLowering, CIRSqrtOpLowering, CIRCeilOpLowering,
CIRExp2OpLowering, CIRExpOpLowering, CIRFAbsOpLowering, CIRAbsOpLowering,
CIRFloorOpLowering, CIRLog10OpLowering, CIRLog2OpLowering,
CIRLogOpLowering, CIRRoundOpLowering, CIRSinOpLowering,
CIRShiftOpLowering, CIRBitClzOpLowering, CIRBitCtzOpLowering,
CIRBitPopcountOpLowering, CIRBitClrsbOpLowering, CIRBitFfsOpLowering,
CIRBitParityOpLowering, CIRIfOpLowering, CIRVectorCreateLowering,
CIRVectorInsertLowering, CIRVectorExtractLowering, CIRVectorCmpOpLowering,
CIRACosOpLowering, CIRASinOpLowering, CIRUnreachableOpLowering,
CIRTanOpLowering, CIRTrapOpLowering>(converter, patterns.getContext());

patterns.add<CIRGetMemberOpLowering>(converter, patterns.getContext(),
layout);
}

static mlir::TypeConverter prepareTypeConverter() {
static mlir::TypeConverter prepareTypeConverter(mlir::DataLayout layout) {
mlir::TypeConverter converter;
converter.addConversion([&](cir::PointerType type) -> mlir::Type {
auto ty = convertTypeForMemory(converter, type.getPointee());
auto pointee = type.getPointee();
auto ty = convertTypeForMemory(converter, pointee);
// FIXME: The pointee type might not be converted (e.g. struct)
if (!ty)
return nullptr;
if (isa<cir::ArrayType>(type.getPointee()))
if (isa<cir::ArrayType>(pointee) || isa<cir::RecordType>(pointee))
return ty;
return mlir::MemRefType::get({}, ty);
});
Expand Down Expand Up @@ -1598,6 +1648,13 @@ static mlir::TypeConverter prepareTypeConverter() {
return nullptr;
return mlir::MemRefType::get(shape, elementType);
});
converter.addConversion([&](cir::RecordType type) -> mlir::Type {
// Reinterpret structs as raw bytes. Don't use tuples as they can't be put
// in memref.
auto size = type.getTypeSize(layout, {});
auto i8 = mlir::IntegerType::get(type.getContext(), /*width=*/8);
return mlir::MemRefType::get(size.getFixedValue(), i8);
});
converter.addConversion([&](cir::VectorType type) -> mlir::Type {
auto ty = converter.convertType(type.getElementType());
return mlir::VectorType::get(type.getSize(), ty);
Expand All @@ -1609,12 +1666,15 @@ void ConvertCIRToMLIRPass::runOnOperation() {
mlir::MLIRContext *context = &getContext();
mlir::ModuleOp theModule = getOperation();

auto converter = prepareTypeConverter();

mlir::DataLayoutAnalysis layoutAnalysis(theModule);
const mlir::DataLayout &layout = layoutAnalysis.getAtOrAbove(theModule);

auto converter = prepareTypeConverter(layout);

mlir::RewritePatternSet patterns(&getContext());

populateCIRLoopToSCFConversionPatterns(patterns, converter);
populateCIRToMLIRConversionPatterns(patterns, converter);
populateCIRToMLIRConversionPatterns(patterns, converter, layout);

mlir::ConversionTarget target(getContext());
target.addLegalOp<mlir::ModuleOp>();
Expand All @@ -1628,10 +1688,11 @@ void ConvertCIRToMLIRPass::runOnOperation() {
// cir dialect, for example the `cir.continue`. If we marked cir as illegal
// here, then MLIR would think any remaining `cir.continue` indicates a
// failure, which is not what we want.

patterns.add<CIRCastOpLowering, CIRIfOpLowering, CIRScopeOpLowering, CIRYieldOpLowering>(converter, context);

if (mlir::failed(mlir::applyPartialConversion(theModule, target,
patterns.add<CIRCastOpLowering, CIRIfOpLowering, CIRScopeOpLowering,
CIRYieldOpLowering>(converter, context);

if (mlir::failed(mlir::applyPartialConversion(theModule, target,
std::move(patterns)))) {
signalPassFailure();
}
Expand Down
25 changes: 25 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/struct.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: cir-opt %s -cir-to-mlir -o %t.mlir
// RUN: FileCheck --input-file=%t.mlir %s

!s32i = !cir.int<s, 32>
!u8i = !cir.int<u, 8>
!u32i = !cir.int<u, 32>
!ty_S = !cir.record<struct "S" {!u8i, !s32i}>

module {
cir.func @test() {
%1 = cir.alloca !ty_S, !cir.ptr<!ty_S>, ["x"] {alignment = 8 : i64}
%3 = cir.get_member %1[0] {name = "c"} : !cir.ptr<!ty_S> -> !cir.ptr<!u8i>
%5 = cir.get_member %1[1] {name = "i"} : !cir.ptr<!ty_S> -> !cir.ptr<!s32i>
cir.return
}

// CHECK: func.func @test() {
// CHECK: %[[alloca:[a-z0-9]+]] = memref.alloca() {alignment = 8 : i64} : memref<8xi8>
// CHECK: %[[zero:[a-z0-9]+]] = arith.constant 0 : index
// CHECK: memref.view %[[alloca]][%[[zero]]][] : memref<8xi8> to memref<i8>
// CHECK: %[[four:[a-z0-9]+]] = arith.constant 4 : index
// CHECK: %view_0 = memref.view %[[alloca]][%[[four]]][] : memref<8xi8> to memref<i32>
// CHECK: return
// CHECK: }
}
Loading