Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
60 changes: 54 additions & 6 deletions lib/Dialect/Arc/ArcTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,66 @@ using namespace mlir;
#define GET_TYPEDEF_CLASSES
#include "circt/Dialect/Arc/ArcTypes.cpp.inc"

unsigned StateType::getBitWidth() {
if (llvm::isa<seq::ClockType>(getType()))
/// Compute the bit width a type will have when allocated as part of the
/// simulator's storage. This includes any padding and alignment that may be
/// necessary once the type has been mapped to LLVM. The idea is for this
/// function to be conservative, such that we provide sufficient storage bytes
/// for any type.
static std::optional<uint64_t> computeLLVMBitWidth(Type type) {
if (isa<seq::ClockType>(type))
return 1;
return hw::getBitWidth(getType());

if (auto intType = dyn_cast<IntegerType>(type))
return intType.getWidth();

if (auto arrayType = dyn_cast<hw::ArrayType>(type)) {
// Compute element width.
auto maybeWidth = computeLLVMBitWidth(arrayType.getElementType());
if (!maybeWidth)
return {};
// Each element must be at least one byte.
auto width = std::max<uint64_t>(*maybeWidth, 8);
// Align elements to their own size, up to 16 bytes.
auto alignment = llvm::bit_ceil(std::min<uint64_t>(width, 16 * 8));
auto alignedWidth = llvm::alignToPowerOf2(width, alignment);
// Multiply by the number of elements in the array.
return arrayType.getNumElements() * alignedWidth;
}

if (auto structType = dyn_cast<hw::StructType>(type)) {
uint64_t structWidth = 0;
uint64_t structAlignment = 8;
for (auto element : structType.getElements()) {
// Compute element width.
auto maybeWidth = computeLLVMBitWidth(element.type);
if (!maybeWidth)
return {};
// Each element must be at least one byte.
auto width = std::max<uint64_t>(*maybeWidth, 8);
// Align elements to their own size, up to 16 bytes.
auto alignment = llvm::bit_ceil(std::min<uint64_t>(width, 16 * 8));
auto alignedWidth = llvm::alignToPowerOf2(width, alignment);
// Pad the struct size to align the element to its alignment need, and add
// the element.
structWidth = llvm::alignToPowerOf2(structWidth, alignment);
structWidth += alignedWidth;
// Keep track of the struct's alignment need.
structAlignment = std::max<uint64_t>(alignment, structAlignment);
}
// Pad the struct size to align its end with the alignment.
return llvm::alignToPowerOf2(structWidth, structAlignment);
}

// We don't know anything about any other types.
return {};
}

unsigned StateType::getBitWidth() { return *computeLLVMBitWidth(getType()); }

LogicalResult
StateType::verify(llvm::function_ref<InFlightDiagnostic()> emitError,
Type innerType) {
if (llvm::isa<seq::ClockType>(innerType))
return success();
if (hw::getBitWidth(innerType) < 0)
if (!computeLLVMBitWidth(innerType))
return emitError() << "state type must have a known bit width; got "
<< innerType;
return success();
Expand Down
18 changes: 18 additions & 0 deletions test/Dialect/Arc/allocate-state.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,21 @@ arc.model @test io !hw.modty<input x : i1, output y : i1> {
}
// CHECK-NEXT: }
}

// CHECK-LABEL: arc.model @StructPadding
// CHECK-NEXT: !arc.storage<4>
arc.model @StructPadding io !hw.modty<> {
^bb0(%arg0: !arc.storage):
// This !hw.struct is only 11 bits wide, but mapped to an !llvm.struct, each
// field gets byte-aligned.
arc.alloc_state %arg0 : (!arc.storage) -> !arc.state<!hw.struct<tag: i5, sign_ext: i1, offset: i3, size: i2>>
}

// CHECK-LABEL: arc.model @ArrayPadding
// CHECK-NEXT: !arc.storage<4>
arc.model @ArrayPadding io !hw.modty<> {
^bb0(%arg0: !arc.storage):
// This !hw.array is only 18 bits wide, but mapped to an !llvm.array, each
// element gets aligned to the next power-of-two.
arc.alloc_state %arg0 : (!arc.storage) -> !arc.state<!hw.array<2xi9>>
}