Skip to content

Commit

Permalink
Only handle local struct for SROA
Browse files Browse the repository at this point in the history
  • Loading branch information
mingzheTerapines committed Jul 5, 2024
1 parent 27d2c2f commit f986f04
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
5 changes: 1 addition & 4 deletions lib/Conversion/ImportVerilog/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ struct RvalueExprVisitor {
return {};
}

if (auto refOp = lhs.getDefiningOp<moore::StructExtractRefOp>())
builder.create<moore::StructInjectOp>(loc, refOp->getOperand(0),
refOp.getFieldNameAttr(), rhs);
else if (expr.isNonBlocking())
if (expr.isNonBlocking())
builder.create<moore::NonBlockingAssignOp>(loc, lhs, rhs);
else
builder.create<moore::BlockingAssignOp>(loc, lhs, rhs);
Expand Down
43 changes: 20 additions & 23 deletions lib/Dialect/Moore/MooreOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,16 @@ DenseMap<Attribute, MemorySlot> VariableOp::destructure(
assert(slot.ptr == getResult());
builder.setInsertionPointAfter(*this);

auto destructurableType = cast<DestructurableTypeInterface>(getType());
DenseMap<Attribute, MemorySlot> slotMap;
SmallVector<Value> inputs;

auto members =
TypeSwitch<Type, ArrayRef<StructLikeMember>>(getType().getNestedType())
.Case<StructType, UnpackedStructType>(
[](auto &type) { return type.getMembers(); })
.Default([](auto) { return ArrayRef<StructLikeMember>(); });
for (const auto &member : members) {
auto type = cast<IntType>(member.type);
if (!type)
continue;
auto input = builder.create<ConstantOp>(getLoc(), type, 0);
inputs.push_back(input);
if (usedIndices.contains(member.name)) {
auto varOp = builder.create<VariableOp>(
getLoc(), RefType::get(member.type), member.name, input.getResult());
slotMap.try_emplace<MemorySlot>(member.name,
{varOp, RefType::get(member.type)});
newAllocators.push_back(varOp);
}
for (Attribute index : usedIndices) {
auto elemType = cast<RefType>(destructurableType.getTypeAtIndex(index));
assert(elemType && "used index must exist");
auto varOp = builder.create<VariableOp>(getLoc(), elemType,
cast<StringAttr>(index), Value());
newAllocators.push_back(varOp);
slotMap.try_emplace<MemorySlot>(index, {varOp.getResult(), elemType});
}
builder.create<StructCreateOp>(getLoc(), getType().getNestedType(), inputs);

return slotMap;
}
Expand Down Expand Up @@ -599,15 +586,25 @@ bool StructExtractRefOp::canRewire(
SmallPtrSetImpl<Attribute> &usedIndices,
SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
const DataLayout &dataLayout) {
return slot.ptr == getInput() && use_empty();
if (slot.ptr != getInput())
return false;
auto index = getFieldNameAttr();
if (!index || !slot.elementPtrs.contains(index))
return false;
usedIndices.insert(index);
return true;
}

DeletionKind
StructExtractRefOp::rewire(const DestructurableMemorySlot &slot,
DenseMap<Attribute, MemorySlot> &subslots,
OpBuilder &builder, const DataLayout &dataLayout) {
auto index = getFieldNameAttr();
const MemorySlot &memorySlot = subslots.at(index);
replaceAllUsesWith(memorySlot.ptr);
getInputMutable().drop();
return DeletionKind::Delete;
erase();
return DeletionKind::Keep;
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ module Expressions;

// CHECK: [[TMP1:%.+]] = moore.struct_extract_ref %struct0, "a" : <struct<{a: i32, b: i32}>> -> <i32>
// CHECK: [[TMP2:%.+]] = moore.read %a : i32
// CHECK: moore.struct_inject %struct0, "a", [[TMP2]] : <struct<{a: i32, b: i32}>> i32
// CHECK: moore.blocking_assign [[TMP1]], [[TMP2]] : i32
struct0.a = a;

// CHECK: [[TMP2:%.+]] = moore.struct_extract %struct0, "b" : <struct<{a: i32, b: i32}>> -> i32
Expand Down
40 changes: 19 additions & 21 deletions test/Dialect/Moore/sroa.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ moore.module @LocalVar() {
%y = moore.variable : <i32>
%z = moore.variable : <i32>
moore.procedure always_comb {
// CHECK: %0 = moore.constant 0 : i32
// CHECK: %a = moore.variable %0 : <i32>
// CHECK: %1 = moore.constant 0 : i32
// CHECK: %b = moore.variable %1 : <i32>
// CHECK: %2 = moore.struct_create %0, %1 : !moore.i32, !moore.i32 -> struct<{a: i32, b: i32}>
// CHECK: %3 = moore.constant 1 : i32
// CHECK: %4 = moore.conversion %3 : !moore.i32 -> !moore.i32
// CHECK: moore.blocking_assign %a, %4 : i32
// CHECK: %5 = moore.constant 4 : i32
// CHECK: %6 = moore.conversion %5 : !moore.i32 -> !moore.i32
// CHECK: moore.blocking_assign %b, %6 : i32
// CHECK: %7 = moore.read %x : i32
// CHECK: %8 = moore.constant 1 : i3
// CHECK: %9 = moore.add %7, %8 : i32
// CHECK: %10 = moore.read %a : i32
// CHECK: moore.blocking_assign %y, %10 : i32
// CHECK: %a = moore.variable : <i32>
// CHECK: %b = moore.variable : <i32>
// CHECK: %0 = moore.constant 1 : i32
// CHECK: %1 = moore.conversion %0 : !moore.i32 -> !moore.i32
// CHECK: moore.blocking_assign %a, %1 : i32
// CHECK: %2 = moore.constant 4 : i32
// CHECK: %3 = moore.conversion %2 : !moore.i32 -> !moore.i32
// CHECK: moore.blocking_assign %b, %3 : i32
// CHECK: %4 = moore.read %x : i32
// CHECK: %5 = moore.constant 1 : i32
// CHECK: %6 = moore.add %4, %5 : i32
// CHECK: moore.blocking_assign %a, %6 : i32
// CHECK: %7 = moore.read %a : i32
// CHECK: moore.blocking_assign %y, %7 : i32
// CHECK: %8 = moore.read %a : i32
// CHECK: %9 = moore.constant 1 : i32
// CHECK: %10 = moore.add %8, %9 : i32
// CHECK: moore.blocking_assign %a, %10 : i32
// CHECK: %11 = moore.read %a : i32
// CHECK: %12 = moore.constant 1 : i32
// CHECK: %13 = moore.add %11, %12 : i32
// CHECK: moore.blocking_assign %a, %13 : i32
// CHECK: %14 = moore.read %a : i32
// CHECK: moore.blocking_assign %z, %14 : i32
// CHECK: moore.blocking_assign %z, %11 : i32
%ii = moore.variable : <struct<{a: i32, b: i32}>>
%0 = moore.struct_extract_ref %ii, "a" : <struct<{a: i32, b: i32}>> -> <i32>
%1 = moore.constant 1 : i32
Expand Down

0 comments on commit f986f04

Please sign in to comment.