Skip to content

Commit

Permalink
[ReuseIR] add nullable type
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Aug 22, 2024
1 parent 6a57f9e commit c4584ed
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
15 changes: 15 additions & 0 deletions reuse-mlir/include/ReuseIR/IR/ReuseIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ def ReuseIR_RegionCtxType : ReuseIR_Type<"RegionCtx", "region_ctx",
}];
}

def ReuseIR_NullableType : ReuseIR_Type<"Nullable", "nullable",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
let summary = "ReuseIR Nullable Type";
let description = [{
`reuse_ir.nullable` is a nullable type. Rc pointers on default at nonnull in ReuseIR.
This is just a type wrapper to indicate that the pointer can be null.
}];
let parameters = (ins
"::mlir::reuse_ir::RcType":$pointer
);
let assemblyFormat = [{
`<` $pointer `>`
}];
}

def ReuseIR_RcBoxType : ReuseIR_Type<"RcBox", "rcbox",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,DeclareTypeInterfaceMethods<ReuseIRCompositeLayoutInterface>]> {
let summary = "ReuseIR Control Box";
Expand Down
6 changes: 4 additions & 2 deletions reuse-mlir/src/cxx/IR/ReuseIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ mlir::reuse_ir::LogicalResult LoadOp::verify() {
if (input.getFreezingKind().getValue() == FreezingKind::nonfreezing)
return emitOpError(
"cannot load a mutable RC pointer through a nonfreezing reference");
targetType = RcType::get(getContext(), mref.getPointee(),
mref.getAtomicKind(), input.getFreezingKind());
targetType = NullableType::get(getContext(),
RcType::get(getContext(), mref.getPointee(),
mref.getAtomicKind(),
input.getFreezingKind()));
} else
targetType = input.getPointee();
if (targetType != getType())
Expand Down
4 changes: 4 additions & 0 deletions reuse-mlir/src/cxx/IR/ReuseIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ GENERATE_POINTER_ALIKE_LAYOUT(RefType)
GENERATE_POINTER_ALIKE_LAYOUT(TokenType)
GENERATE_POINTER_ALIKE_LAYOUT(MRefType)
GENERATE_POINTER_ALIKE_LAYOUT(RegionCtxType)
GENERATE_POINTER_ALIKE_LAYOUT(NullableType)
#pragma pop_macro("GENERATE_POINTER_ALIKE_LAYOUT")

// RcBox DataLayoutInterface:
Expand Down Expand Up @@ -343,6 +344,9 @@ void populateLLVMTypeConverter(CompositeLayoutCache &cache,
converter.addConversion([](RegionCtxType type) -> Type {
return mlir::LLVM::LLVMPointerType::get(type.getContext());
});
converter.addConversion([](NullableType type) -> Type {
return mlir::LLVM::LLVMPointerType::get(type.getContext());
});
converter.addConversion(
[&converter, &cache](ReuseIRCompositeLayoutInterface type) -> Type {
return cache.get(type).getLLVMType(converter);
Expand Down
32 changes: 17 additions & 15 deletions reuse-mlir/test/integration/lowering/load_op_lowering.mlir
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
// RUN: %reuse-opt %s -convert-reuse-ir-to-llvm | %FileCheck %s
!test = !reuse_ir.composite<!reuse_ir.composite<i32, i32, f128>, i32>
!mref = !reuse_ir.mref<f128, nonatomic>
!inner = !reuse_ir.composite<i32, i32, !mref>
!test = !reuse_ir.composite<!inner, i32>
!array = !reuse_ir.array<!test, 16, 16>
module @test attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<f80, dense<128> : vector<2xi64>>, #dlti.dl_entry<i128, dense<128> : vector<2xi64>>, #dlti.dl_entry<i32, dense<32> : vector<2xi64>>, #dlti.dl_entry<f128, dense<128> : vector<2xi64>>, #dlti.dl_entry<f64, dense<64> : vector<2xi64>>, #dlti.dl_entry<f16, dense<16> : vector<2xi64>>, #dlti.dl_entry<i1, dense<8> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr, dense<64> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<270>, dense<32> : vector<4xi64>>, #dlti.dl_entry<i8, dense<8> : vector<2xi64>>, #dlti.dl_entry<i16, dense<16> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr<272>, dense<64> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<271>, dense<32> : vector<4xi64>>, #dlti.dl_entry<i64, dense<64> : vector<2xi64>>, #dlti.dl_entry<"dlti.stack_alignment", 128 : i64>, #dlti.dl_entry<"dlti.endianness", "little">>, llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"}
{
func.func @array_projection(%0: !reuse_ir.rc<!array, nonatomic, nonfreezing>) -> f128 {
func.func @array_projection(%0: !reuse_ir.rc<!array, nonatomic, frozen>) -> !reuse_ir.nullable<<f128, nonatomic, frozen>> {
%1 = reuse_ir.borrow %0 :
!reuse_ir.rc<!array, nonatomic, nonfreezing>
-> !reuse_ir.ref<!array, nonfreezing>
// CHECK: %[[REG0:[0-9a-z]+]] = llvm.getelementptr %{{[0-9a-z]+}}[13] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<16 x struct<(struct<(i32, i32, array<8 x i8>, f128)>, i32, array<12 x i8>)>>
!reuse_ir.rc<!array, nonatomic, frozen>
-> !reuse_ir.ref<!array, frozen>
// CHECK: %[[REG0:[0-9a-z]+]] = llvm.getelementptr %{{[0-9a-z]+}}[13] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<16 x struct<(struct<(i32, i32, ptr)>, i32, array<4 x i8>)>>
%2 = reuse_ir.proj %1[13] :
!reuse_ir.ref<!array, nonfreezing> -> !reuse_ir.ref<!reuse_ir.array<!test, 16>, nonfreezing>
// CHECK: %[[REG1:[0-9a-z]+]] = llvm.getelementptr %[[REG0]][7] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(struct<(i32, i32, array<8 x i8>, f128)>, i32, array<12 x i8>)>
!reuse_ir.ref<!array, frozen> -> !reuse_ir.ref<!reuse_ir.array<!test, 16>, frozen>
// CHECK: %[[REG1:[0-9a-z]+]] = llvm.getelementptr %[[REG0]][7] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(struct<(i32, i32, ptr)>, i32, array<4 x i8>)>
%3 = reuse_ir.proj %2[7] :
!reuse_ir.ref<!reuse_ir.array<!test, 16>, nonfreezing> -> !reuse_ir.ref<!test, nonfreezing>
// CHECK: %[[REG2:[0-9a-z]+]] = llvm.getelementptr %[[REG1]][0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(struct<(i32, i32, array<8 x i8>, f128)>, i32, array<12 x i8>)>
!reuse_ir.ref<!reuse_ir.array<!test, 16>, frozen> -> !reuse_ir.ref<!test, frozen>
// CHECK: %[[REG2:[0-9a-z]+]] = llvm.getelementptr %[[REG1]][0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(struct<(i32, i32, ptr)>, i32, array<4 x i8>)>
%4 = reuse_ir.proj %3[0] :
!reuse_ir.ref<!test, nonfreezing> -> !reuse_ir.ref<!reuse_ir.composite<i32, i32, f128>, nonfreezing>
// CHECK: %[[REG3:[0-9a-z]+]] = llvm.getelementptr %[[REG2]][0, 3] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, array<8 x i8>, f128)>
!reuse_ir.ref<!test, frozen> -> !reuse_ir.ref<!inner, frozen>
// CHECK: %[[REG3:[0-9a-z]+]] = llvm.getelementptr %[[REG2]][0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, ptr)>
%5 = reuse_ir.proj %4[2] :
!reuse_ir.ref<!reuse_ir.composite<i32, i32, f128>, nonfreezing> -> !reuse_ir.ref<f128, nonfreezing>
// CHECK: %{{[0-9a-z]+}} = llvm.load %[[REG3]] {alignment = 16 : i64} : !llvm.ptr -> f128
!reuse_ir.ref<!inner, frozen> -> !reuse_ir.ref<!mref, frozen>
// CHECK: %{{[0-9a-z]+}} = llvm.load %[[REG3]] {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
%6 = reuse_ir.load %5 :
!reuse_ir.ref<f128, nonfreezing> -> f128
return %6 : f128
!reuse_ir.ref<!mref, frozen> -> !reuse_ir.nullable<<f128, nonatomic, frozen>>
return %6 : !reuse_ir.nullable<<f128, nonatomic, frozen>>
}
}

0 comments on commit c4584ed

Please sign in to comment.