18#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
19#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
20#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
21#include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
22#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
23#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
24#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
25#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h"
26#include "mlir/Dialect/Func/IR/FuncOps.h"
27#include "mlir/Dialect/Index/IR/IndexOps.h"
28#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
29#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
30#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
31#include "mlir/Dialect/SCF/IR/SCF.h"
32#include "mlir/IR/BuiltinDialect.h"
33#include "mlir/Pass/Pass.h"
34#include "mlir/Transforms/DialectConversion.h"
35#include "llvm/Support/Debug.h"
37#define DEBUG_TYPE "lower-arc-to-llvm"
40#define GEN_PASS_DEF_LOWERARCTOLLVM
41#include "circt/Conversion/Passes.h.inc"
54 return modelName +
"_eval";
60 using OpConversionPattern::OpConversionPattern;
62 matchAndRewrite(arc::ModelOp op, OpAdaptor adaptor,
63 ConversionPatternRewriter &rewriter)
const final {
65 IRRewriter::InsertionGuard guard(rewriter);
66 rewriter.setInsertionPointToEnd(&op.getBodyBlock());
67 rewriter.create<func::ReturnOp>(op.getLoc());
72 rewriter.getFunctionType(op.getBody().getArgumentTypes(), {});
74 rewriter.create<mlir::func::FuncOp>(op.getLoc(), funcName, funcType);
75 rewriter.inlineRegionBefore(op.getRegion(), func.getBody(), func.end());
81struct AllocStorageOpLowering
83 using OpConversionPattern::OpConversionPattern;
85 matchAndRewrite(arc::AllocStorageOp op, OpAdaptor adaptor,
86 ConversionPatternRewriter &rewriter)
const final {
87 auto type = typeConverter->convertType(op.getType());
88 if (!op.getOffset().has_value())
90 rewriter.replaceOpWithNewOp<LLVM::GEPOp>(op, type, rewriter.getI8Type(),
92 LLVM::GEPArg(*op.getOffset()));
97template <
class ConcreteOp>
101 using OpAdaptor =
typename ConcreteOp::Adaptor;
104 matchAndRewrite(ConcreteOp op, OpAdaptor adaptor,
105 ConversionPatternRewriter &rewriter)
const final {
107 auto offsetAttr = op->template getAttrOfType<IntegerAttr>(
"offset");
110 Value ptr = rewriter.create<LLVM::GEPOp>(
111 op->getLoc(), adaptor.getStorage().getType(), rewriter.getI8Type(),
112 adaptor.getStorage(),
113 LLVM::GEPArg(offsetAttr.getValue().getZExtValue()));
114 rewriter.replaceOp(op, ptr);
120 using OpConversionPattern::OpConversionPattern;
122 matchAndRewrite(arc::StateReadOp op, OpAdaptor adaptor,
123 ConversionPatternRewriter &rewriter)
const final {
124 auto type = typeConverter->convertType(op.getType());
125 rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, type, adaptor.getState());
131 using OpConversionPattern::OpConversionPattern;
133 matchAndRewrite(arc::StateWriteOp op, OpAdaptor adaptor,
134 ConversionPatternRewriter &rewriter)
const final {
135 if (adaptor.getCondition()) {
136 rewriter.replaceOpWithNewOp<scf::IfOp>(
137 op, adaptor.getCondition(), [&](
auto &builder,
auto loc) {
138 builder.template create<LLVM::StoreOp>(loc, adaptor.getValue(),
140 builder.template create<scf::YieldOp>(loc);
143 rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getValue(),
151 using OpConversionPattern::OpConversionPattern;
153 matchAndRewrite(arc::AllocMemoryOp op, OpAdaptor adaptor,
154 ConversionPatternRewriter &rewriter)
const final {
155 auto offsetAttr = op->getAttrOfType<IntegerAttr>(
"offset");
158 Value ptr = rewriter.create<LLVM::GEPOp>(
159 op.getLoc(), adaptor.getStorage().getType(), rewriter.getI8Type(),
160 adaptor.getStorage(),
161 LLVM::GEPArg(offsetAttr.getValue().getZExtValue()));
163 rewriter.replaceOp(op, ptr);
169 using OpConversionPattern::OpConversionPattern;
171 matchAndRewrite(arc::StorageGetOp op, OpAdaptor adaptor,
172 ConversionPatternRewriter &rewriter)
const final {
173 Value offset = rewriter.create<LLVM::ConstantOp>(
174 op.getLoc(), rewriter.getI32Type(), op.getOffsetAttr());
175 Value ptr = rewriter.create<LLVM::GEPOp>(
176 op.getLoc(), adaptor.getStorage().getType(), rewriter.getI8Type(),
177 adaptor.getStorage(), offset);
178 rewriter.replaceOp(op, ptr);
188static MemoryAccess prepareMemoryAccess(Location loc, Value memory,
189 Value address, MemoryType type,
190 ConversionPatternRewriter &rewriter) {
191 auto zextAddrType = rewriter.getIntegerType(
192 cast<IntegerType>(address.getType()).getWidth() + 1);
193 Value
addr = rewriter.create<LLVM::ZExtOp>(loc, zextAddrType, address);
194 Value addrLimit = rewriter.create<LLVM::ConstantOp>(
195 loc, zextAddrType, rewriter.getI32IntegerAttr(type.getNumWords()));
196 Value withinBounds = rewriter.create<LLVM::ICmpOp>(
197 loc, LLVM::ICmpPredicate::ult,
addr, addrLimit);
198 Value ptr = rewriter.create<LLVM::GEPOp>(
199 loc, LLVM::LLVMPointerType::get(memory.getContext()),
200 rewriter.getIntegerType(type.getStride() * 8), memory, ValueRange{
addr});
201 return {ptr, withinBounds};
205 using OpConversionPattern::OpConversionPattern;
207 matchAndRewrite(arc::MemoryReadOp op, OpAdaptor adaptor,
208 ConversionPatternRewriter &rewriter)
const final {
209 auto type = typeConverter->convertType(op.getType());
210 auto memoryType = cast<MemoryType>(op.getMemory().getType());
212 prepareMemoryAccess(op.getLoc(), adaptor.getMemory(),
213 adaptor.getAddress(), memoryType, rewriter);
217 rewriter.replaceOpWithNewOp<scf::IfOp>(
218 op, access.withinBounds,
219 [&](
auto &builder,
auto loc) {
220 Value loadOp = builder.template create<LLVM::LoadOp>(
221 loc, memoryType.getWordType(), access.ptr);
222 builder.template create<scf::YieldOp>(loc, loadOp);
224 [&](
auto &builder,
auto loc) {
225 Value zeroValue = builder.template create<LLVM::ConstantOp>(
226 loc, type, builder.getI64IntegerAttr(0));
227 builder.template create<scf::YieldOp>(loc, zeroValue);
234 using OpConversionPattern::OpConversionPattern;
236 matchAndRewrite(arc::MemoryWriteOp op, OpAdaptor adaptor,
237 ConversionPatternRewriter &rewriter)
const final {
238 auto access = prepareMemoryAccess(
239 op.getLoc(), adaptor.getMemory(), adaptor.getAddress(),
240 cast<MemoryType>(op.getMemory().getType()), rewriter);
241 auto enable = access.withinBounds;
242 if (adaptor.getEnable())
243 enable = rewriter.create<LLVM::AndOp>(op.getLoc(), adaptor.getEnable(),
247 rewriter.replaceOpWithNewOp<scf::IfOp>(
248 op, enable, [&](
auto &builder,
auto loc) {
249 builder.template create<LLVM::StoreOp>(loc, adaptor.getData(),
251 builder.template create<scf::YieldOp>(loc);
259 using OpConversionPattern::OpConversionPattern;
261 matchAndRewrite(seq::ClockGateOp op, OpAdaptor adaptor,
262 ConversionPatternRewriter &rewriter)
const final {
263 rewriter.replaceOpWithNewOp<LLVM::AndOp>(op, adaptor.getInput(),
264 adaptor.getEnable());
271 using OpConversionPattern::OpConversionPattern;
273 matchAndRewrite(seq::ClockInverterOp op, OpAdaptor adaptor,
274 ConversionPatternRewriter &rewriter)
const final {
275 auto constTrue = rewriter.create<LLVM::ConstantOp>(op->getLoc(),
276 rewriter.getI1Type(), 1);
277 rewriter.replaceOpWithNewOp<LLVM::XOrOp>(op, adaptor.getInput(), constTrue);
283 using OpConversionPattern::OpConversionPattern;
285 matchAndRewrite(arc::ZeroCountOp op, OpAdaptor adaptor,
286 ConversionPatternRewriter &rewriter)
const override {
288 IntegerAttr isZeroPoison = rewriter.getBoolAttr(
true);
290 if (op.getPredicate() == arc::ZeroCountPredicate::leading) {
291 rewriter.replaceOpWithNewOp<LLVM::CountLeadingZerosOp>(
292 op, adaptor.getInput().getType(), adaptor.getInput(), isZeroPoison);
296 rewriter.replaceOpWithNewOp<LLVM::CountTrailingZerosOp>(
297 op, adaptor.getInput().getType(), adaptor.getInput(), isZeroPoison);
303 using OpConversionPattern::OpConversionPattern;
305 matchAndRewrite(seq::ConstClockOp op, OpAdaptor adaptor,
306 ConversionPatternRewriter &rewriter)
const override {
307 rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(
308 op, rewriter.getI1Type(),
static_cast<int64_t
>(op.getValue()));
313template <
typename OpTy>
316 using OpAdaptor =
typename OpTy::Adaptor;
318 matchAndRewrite(OpTy op, OpAdaptor adaptor,
319 ConversionPatternRewriter &rewriter)
const override {
320 rewriter.replaceOp(op, adaptor.getInput());
334 size_t numStateBytes;
335 llvm::DenseMap<StringRef, StateInfo> states;
336 mlir::FlatSymbolRefAttr initialFnSymbol;
337 mlir::FlatSymbolRefAttr finalFnSymbol;
340template <
typename OpTy>
342 ModelAwarePattern(
const TypeConverter &typeConverter, MLIRContext *context,
343 llvm::DenseMap<StringRef, ModelInfoMap> &modelInfo)
345 modelInfo(modelInfo) {}
348 Value createPtrToPortState(ConversionPatternRewriter &rewriter, Location loc,
349 Value state,
const StateInfo &port)
const {
350 MLIRContext *ctx = rewriter.getContext();
351 return rewriter.create<LLVM::GEPOp>(loc, LLVM::LLVMPointerType::get(ctx),
352 IntegerType::get(ctx, 8), state,
353 LLVM::GEPArg(port.
offset));
356 llvm::DenseMap<StringRef, ModelInfoMap> &modelInfo;
361struct SimInstantiateOpLowering
362 :
public ModelAwarePattern<arc::SimInstantiateOp> {
363 using ModelAwarePattern::ModelAwarePattern;
366 matchAndRewrite(arc::SimInstantiateOp op, OpAdaptor adaptor,
367 ConversionPatternRewriter &rewriter)
const final {
368 auto modelIt = modelInfo.find(
369 cast<SimModelInstanceType>(op.getBody().getArgument(0).getType())
372 ModelInfoMap &model = modelIt->second;
374 ModuleOp moduleOp = op->getParentOfType<ModuleOp>();
378 ConversionPatternRewriter::InsertionGuard guard(rewriter);
382 Type convertedIndex = typeConverter->convertType(rewriter.getIndexType());
384 LLVM::LLVMFuncOp mallocFunc =
385 LLVM::lookupOrCreateMallocFn(moduleOp, convertedIndex);
386 LLVM::LLVMFuncOp freeFunc = LLVM::lookupOrCreateFreeFn(moduleOp);
388 Location loc = op.getLoc();
389 Value numStateBytes = rewriter.create<LLVM::ConstantOp>(
390 loc, convertedIndex, model.numStateBytes);
393 .create<LLVM::CallOp>(loc, mallocFunc, ValueRange{numStateBytes})
396 rewriter.create<LLVM::ConstantOp>(loc, rewriter.getI8Type(), 0);
397 rewriter.create<LLVM::MemsetOp>(loc, allocated, zero, numStateBytes,
false);
400 if (model.initialFnSymbol) {
401 auto initialFnType = LLVM::LLVMFunctionType::get(
402 LLVM::LLVMVoidType::get(op.getContext()),
403 {LLVM::LLVMPointerType::get(op.getContext())});
404 rewriter.create<LLVM::CallOp>(loc, initialFnType, model.initialFnSymbol,
405 ValueRange{allocated});
409 rewriter.inlineBlockBefore(&adaptor.getBody().getBlocks().front(), op,
413 if (model.finalFnSymbol) {
414 auto finalFnType = LLVM::LLVMFunctionType::get(
415 LLVM::LLVMVoidType::get(op.getContext()),
416 {LLVM::LLVMPointerType::get(op.getContext())});
417 rewriter.create<LLVM::CallOp>(loc, finalFnType, model.finalFnSymbol,
418 ValueRange{allocated});
421 rewriter.create<LLVM::CallOp>(loc, freeFunc, ValueRange{allocated});
422 rewriter.eraseOp(op);
428struct SimSetInputOpLowering :
public ModelAwarePattern<arc::SimSetInputOp> {
429 using ModelAwarePattern::ModelAwarePattern;
432 matchAndRewrite(arc::SimSetInputOp op, OpAdaptor adaptor,
433 ConversionPatternRewriter &rewriter)
const final {
435 modelInfo.find(cast<SimModelInstanceType>(op.getInstance().getType())
438 ModelInfoMap &model = modelIt->second;
440 auto portIt = model.states.find(op.getInput());
441 if (portIt == model.states.end()) {
444 rewriter.eraseOp(op);
449 Value statePtr = createPtrToPortState(rewriter, op.getLoc(),
450 adaptor.getInstance(), port);
451 rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getValue(),
458struct SimGetPortOpLowering :
public ModelAwarePattern<arc::SimGetPortOp> {
459 using ModelAwarePattern::ModelAwarePattern;
462 matchAndRewrite(arc::SimGetPortOp op, OpAdaptor adaptor,
463 ConversionPatternRewriter &rewriter)
const final {
465 modelInfo.find(cast<SimModelInstanceType>(op.getInstance().getType())
468 ModelInfoMap &model = modelIt->second;
470 auto type = typeConverter->convertType(op.getValue().getType());
473 auto portIt = model.states.find(op.getPort());
474 if (portIt == model.states.end()) {
477 rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(op, type, 0);
482 Value statePtr = createPtrToPortState(rewriter, op.getLoc(),
483 adaptor.getInstance(), port);
484 rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, type, statePtr);
490struct SimStepOpLowering :
public ModelAwarePattern<arc::SimStepOp> {
491 using ModelAwarePattern::ModelAwarePattern;
494 matchAndRewrite(arc::SimStepOp op, OpAdaptor adaptor,
495 ConversionPatternRewriter &rewriter)
const final {
496 StringRef modelName = cast<SimModelInstanceType>(op.getInstance().getType())
500 StringAttr evalFunc =
502 rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, std::nullopt, evalFunc,
503 adaptor.getInstance());
512struct SimEmitValueOpLowering
514 using OpConversionPattern::OpConversionPattern;
517 matchAndRewrite(arc::SimEmitValueOp op, OpAdaptor adaptor,
518 ConversionPatternRewriter &rewriter)
const final {
519 auto valueType = dyn_cast<IntegerType>(adaptor.getValue().getType());
523 Location loc = op.getLoc();
525 ModuleOp moduleOp = op->getParentOfType<ModuleOp>();
532 Value toPrint = adaptor.getValue();
533 DataLayout layout = DataLayout::closest(op);
534 llvm::TypeSize sizeOfSizeT =
535 layout.getTypeSizeInBits(rewriter.getIndexType());
536 assert(!sizeOfSizeT.isScalable() &&
537 sizeOfSizeT.getFixedValue() <= std::numeric_limits<unsigned>::max());
538 bool truncated =
false;
539 if (valueType.getWidth() > sizeOfSizeT) {
540 toPrint = rewriter.create<LLVM::TruncOp>(
541 loc, IntegerType::get(getContext(), sizeOfSizeT.getFixedValue()),
544 }
else if (valueType.getWidth() < sizeOfSizeT)
545 toPrint = rewriter.create<LLVM::ZExtOp>(
546 loc, IntegerType::get(getContext(), sizeOfSizeT.getFixedValue()),
550 auto printfFunc = LLVM::lookupOrCreateFn(
551 moduleOp,
"printf", LLVM::LLVMPointerType::get(getContext()),
552 LLVM::LLVMVoidType::get(getContext()),
true);
555 SmallString<16> formatStrName{
"_arc_sim_emit_"};
556 formatStrName.append(truncated ?
"trunc_" :
"full_");
557 formatStrName.append(adaptor.getValueName());
558 LLVM::GlobalOp formatStrGlobal;
559 if (!(formatStrGlobal =
560 moduleOp.lookupSymbol<LLVM::GlobalOp>(formatStrName))) {
561 ConversionPatternRewriter::InsertionGuard insertGuard(rewriter);
563 SmallString<16> formatStr = adaptor.getValueName();
564 formatStr.append(
" = ");
566 formatStr.append(
"(truncated) ");
567 formatStr.append(
"%zx\n");
568 SmallVector<char> formatStrVec{formatStr.begin(), formatStr.end()};
569 formatStrVec.push_back(0);
571 rewriter.setInsertionPointToStart(moduleOp.getBody());
573 LLVM::LLVMArrayType::get(rewriter.getI8Type(), formatStrVec.size());
574 formatStrGlobal = rewriter.create<LLVM::GlobalOp>(
575 loc, globalType,
true, LLVM::Linkage::Internal,
576 formatStrName, rewriter.getStringAttr(formatStrVec),
580 Value formatStrGlobalPtr =
581 rewriter.create<LLVM::AddressOfOp>(loc, formatStrGlobal);
582 rewriter.replaceOpWithNewOp<LLVM::CallOp>(
583 op, printfFunc, ValueRange{formatStrGlobalPtr, toPrint});
596struct LowerArcToLLVMPass
597 :
public circt::impl::LowerArcToLLVMBase<LowerArcToLLVMPass> {
598 void runOnOperation()
override;
602void LowerArcToLLVMPass::runOnOperation() {
614 LLVMConversionTarget target(getContext());
615 target.addLegalOp<mlir::ModuleOp>();
616 target.addLegalOp<scf::YieldOp>();
619 LLVMTypeConverter converter(&getContext());
620 converter.addConversion([&](seq::ClockType type) {
621 return IntegerType::get(type.getContext(), 1);
623 converter.addConversion([&](StorageType type) {
624 return LLVM::LLVMPointerType::get(type.getContext());
626 converter.addConversion([&](MemoryType type) {
627 return LLVM::LLVMPointerType::get(type.getContext());
629 converter.addConversion([&](StateType type) {
630 return LLVM::LLVMPointerType::get(type.getContext());
632 converter.addConversion([&](SimModelInstanceType type) {
633 return LLVM::LLVMPointerType::get(type.getContext());
637 RewritePatternSet
patterns(&getContext());
640 populateSCFToControlFlowConversionPatterns(
patterns);
641 populateFuncToLLVMConversionPatterns(converter,
patterns);
642 cf::populateControlFlowToLLVMConversionPatterns(converter,
patterns);
643 arith::populateArithToLLVMConversionPatterns(converter,
patterns);
644 index::populateIndexToLLVMConversionPatterns(converter,
patterns);
645 populateAnyFunctionOpInterfaceTypeConversionPattern(
patterns, converter);
648 DenseMap<std::pair<Type, ArrayAttr>, LLVM::GlobalOp> constAggregateGlobalsMap;
650 constAggregateGlobalsMap);
658 AllocMemoryOpLowering,
659 AllocStateLikeOpLowering<arc::AllocStateOp>,
660 AllocStateLikeOpLowering<arc::RootInputOp>,
661 AllocStateLikeOpLowering<arc::RootOutputOp>,
662 AllocStorageOpLowering,
665 MemoryReadOpLowering,
666 MemoryWriteOpLowering,
668 ReplaceOpWithInputPattern<seq::ToClockOp>,
669 ReplaceOpWithInputPattern<seq::FromClockOp>,
670 SeqConstClockLowering,
671 SimEmitValueOpLowering,
673 StateWriteOpLowering,
674 StorageGetOpLowering,
676 >(converter, &getContext());
679 SmallVector<ModelInfo> models;
685 llvm::DenseMap<StringRef, ModelInfoMap> modelMap(models.size());
687 llvm::DenseMap<StringRef, StateInfo> states(modelInfo.states.size());
688 for (
StateInfo &stateInfo : modelInfo.states)
689 states.insert({stateInfo.name, stateInfo});
692 ModelInfoMap{modelInfo.numStateBytes, std::move(states),
693 modelInfo.initialFnSym, modelInfo.finalFnSym}});
696 patterns.add<SimInstantiateOpLowering, SimSetInputOpLowering,
697 SimGetPortOpLowering, SimStepOpLowering>(
698 converter, &getContext(), modelMap);
701 if (failed(applyFullConversion(getOperation(), target, std::move(
patterns))))
706 return std::make_unique<LowerArcToLLVMPass>();
assert(baseType &&"element must be base type")
static llvm::Twine evalSymbolFromModelName(StringRef modelName)
A namespace that is used to store existing names and generate new names in some scope within the IR.
void add(mlir::ModuleOp module)
void addDefinitions(mlir::Operation *top)
Populate the symbol cache with all symbol-defining operations within the 'top' operation.
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
mlir::LogicalResult collectModels(mlir::ModuleOp module, llvm::SmallVector< ModelInfo > &models)
Collects information about all Arc models in the provided module, and adds it to models.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
void populateHWToLLVMConversionPatterns(mlir::LLVMTypeConverter &converter, RewritePatternSet &patterns, Namespace &globals, DenseMap< std::pair< Type, ArrayAttr >, mlir::LLVM::GlobalOp > &constAggregateGlobalsMap)
Get the HW to LLVM conversion patterns.
void populateCombToArithConversionPatterns(TypeConverter &converter, RewritePatternSet &patterns)
void populateCombToLLVMConversionPatterns(mlir::LLVMTypeConverter &converter, RewritePatternSet &patterns)
Get the Comb to LLVM conversion patterns.
void populateHWToLLVMTypeConversions(mlir::LLVMTypeConverter &converter)
Get the HW to LLVM type conversions.
std::unique_ptr< OperationPass< ModuleOp > > createLowerArcToLLVMPass()
Gathers information about a given Arc model.
Gathers information about a given Arc state.