CIRCT 23.0.0git
Loading...
Searching...
No Matches
LowerArcToLLVM.cpp
Go to the documentation of this file.
1//===- LowerArcToLLVM.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
26#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
27#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
28#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
29#include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
30#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
31#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
32#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
33#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
34#include "mlir/Dialect/Func/IR/FuncOps.h"
35#include "mlir/Dialect/Index/IR/IndexOps.h"
36#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
37#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
38#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
39#include "mlir/Dialect/SCF/IR/SCF.h"
40#include "mlir/IR/BuiltinDialect.h"
41#include "mlir/Pass/Pass.h"
42#include "mlir/Transforms/DialectConversion.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/FormatVariadic.h"
45
46#include <cstddef>
47
48#define DEBUG_TYPE "lower-arc-to-llvm"
49
50namespace circt {
51#define GEN_PASS_DEF_LOWERARCTOLLVM
52#include "circt/Conversion/Passes.h.inc"
53} // namespace circt
54
55using namespace mlir;
56using namespace circt;
57using namespace arc;
58using namespace hw;
59using namespace runtime;
60
61//===----------------------------------------------------------------------===//
62// Lowering Patterns
63//===----------------------------------------------------------------------===//
64
65static llvm::Twine evalSymbolFromModelName(StringRef modelName) {
66 return modelName + "_eval";
67}
68
69namespace {
70
71struct ModelOpLowering : public OpConversionPattern<arc::ModelOp> {
72 using OpConversionPattern::OpConversionPattern;
73 LogicalResult
74 matchAndRewrite(arc::ModelOp op, OpAdaptor adaptor,
75 ConversionPatternRewriter &rewriter) const final {
76 {
77 IRRewriter::InsertionGuard guard(rewriter);
78 rewriter.setInsertionPointToEnd(&op.getBodyBlock());
79 func::ReturnOp::create(rewriter, op.getLoc());
80 }
81 auto funcName =
82 rewriter.getStringAttr(evalSymbolFromModelName(op.getName()));
83 auto funcType =
84 rewriter.getFunctionType(op.getBody().getArgumentTypes(), {});
85 auto func =
86 mlir::func::FuncOp::create(rewriter, op.getLoc(), funcName, funcType);
87 rewriter.inlineRegionBefore(op.getRegion(), func.getBody(), func.end());
88 rewriter.eraseOp(op);
89 return success();
90 }
91};
92
93struct AllocStorageOpLowering
94 : public OpConversionPattern<arc::AllocStorageOp> {
95 using OpConversionPattern::OpConversionPattern;
96 LogicalResult
97 matchAndRewrite(arc::AllocStorageOp op, OpAdaptor adaptor,
98 ConversionPatternRewriter &rewriter) const final {
99 auto type = typeConverter->convertType(op.getType());
100 if (!op.getOffset().has_value())
101 return failure();
102 rewriter.replaceOpWithNewOp<LLVM::GEPOp>(op, type, rewriter.getI8Type(),
103 adaptor.getInput(),
104 LLVM::GEPArg(*op.getOffset()));
105 return success();
106 }
107};
108
109template <class ConcreteOp>
110struct AllocStateLikeOpLowering : public OpConversionPattern<ConcreteOp> {
112 using OpConversionPattern<ConcreteOp>::typeConverter;
113 using OpAdaptor = typename ConcreteOp::Adaptor;
114
115 LogicalResult
116 matchAndRewrite(ConcreteOp op, OpAdaptor adaptor,
117 ConversionPatternRewriter &rewriter) const final {
118 // Get a pointer to the correct offset in the storage.
119 auto offsetAttr = op->template getAttrOfType<IntegerAttr>("offset");
120 if (!offsetAttr)
121 return failure();
122 Value ptr = LLVM::GEPOp::create(
123 rewriter, op->getLoc(), adaptor.getStorage().getType(),
124 rewriter.getI8Type(), adaptor.getStorage(),
125 LLVM::GEPArg(offsetAttr.getValue().getZExtValue()));
126 rewriter.replaceOp(op, ptr);
127 return success();
128 }
129};
130
131struct StateReadOpLowering : public OpConversionPattern<arc::StateReadOp> {
132 using OpConversionPattern::OpConversionPattern;
133 LogicalResult
134 matchAndRewrite(arc::StateReadOp op, OpAdaptor adaptor,
135 ConversionPatternRewriter &rewriter) const final {
136 auto type = typeConverter->convertType(op.getType());
137 rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, type, adaptor.getState());
138 return success();
139 }
140};
141
142struct StateWriteOpLowering : public OpConversionPattern<arc::StateWriteOp> {
143 using OpConversionPattern::OpConversionPattern;
144 LogicalResult
145 matchAndRewrite(arc::StateWriteOp op, OpAdaptor adaptor,
146 ConversionPatternRewriter &rewriter) const final {
147 if (adaptor.getCondition()) {
148 rewriter.replaceOpWithNewOp<scf::IfOp>(
149 op, adaptor.getCondition(), [&](auto &builder, auto loc) {
150 LLVM::StoreOp::create(builder, loc, adaptor.getValue(),
151 adaptor.getState());
152 scf::YieldOp::create(builder, loc);
153 });
154 } else {
155 rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getValue(),
156 adaptor.getState());
157 }
158 return success();
159 }
160};
161
162//===----------------------------------------------------------------------===//
163// Time Operations Lowering
164//===----------------------------------------------------------------------===//
165
166struct CurrentTimeOpLowering : public OpConversionPattern<arc::CurrentTimeOp> {
167 using OpConversionPattern::OpConversionPattern;
168 LogicalResult
169 matchAndRewrite(arc::CurrentTimeOp op, OpAdaptor adaptor,
170 ConversionPatternRewriter &rewriter) const final {
171 // Time is stored at offset 0 in storage (no offset needed).
172 Value ptr = adaptor.getStorage();
173 rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, rewriter.getI64Type(), ptr);
174 return success();
175 }
176};
177
178// `llhd.int_to_time` is a no-op
179struct IntToTimeOpLowering : public OpConversionPattern<llhd::IntToTimeOp> {
180 using OpConversionPattern::OpConversionPattern;
181 LogicalResult
182 matchAndRewrite(llhd::IntToTimeOp op, OpAdaptor adaptor,
183 ConversionPatternRewriter &rewriter) const final {
184 rewriter.replaceOp(op, adaptor.getInput());
185 return success();
186 }
187};
188
189// `llhd.time_to_int` is a no-op
190struct TimeToIntOpLowering : public OpConversionPattern<llhd::TimeToIntOp> {
191 using OpConversionPattern::OpConversionPattern;
192 LogicalResult
193 matchAndRewrite(llhd::TimeToIntOp op, OpAdaptor adaptor,
194 ConversionPatternRewriter &rewriter) const final {
195 rewriter.replaceOp(op, adaptor.getInput());
196 return success();
197 }
198};
199
200//===----------------------------------------------------------------------===//
201// Memory and Storage Lowering
202//===----------------------------------------------------------------------===//
203
204struct AllocMemoryOpLowering : public OpConversionPattern<arc::AllocMemoryOp> {
205 using OpConversionPattern::OpConversionPattern;
206 LogicalResult
207 matchAndRewrite(arc::AllocMemoryOp op, OpAdaptor adaptor,
208 ConversionPatternRewriter &rewriter) const final {
209 auto offsetAttr = op->getAttrOfType<IntegerAttr>("offset");
210 if (!offsetAttr)
211 return failure();
212 Value ptr = LLVM::GEPOp::create(
213 rewriter, op.getLoc(), adaptor.getStorage().getType(),
214 rewriter.getI8Type(), adaptor.getStorage(),
215 LLVM::GEPArg(offsetAttr.getValue().getZExtValue()));
216
217 rewriter.replaceOp(op, ptr);
218 return success();
219 }
220};
221
222struct StorageGetOpLowering : public OpConversionPattern<arc::StorageGetOp> {
223 using OpConversionPattern::OpConversionPattern;
224 LogicalResult
225 matchAndRewrite(arc::StorageGetOp op, OpAdaptor adaptor,
226 ConversionPatternRewriter &rewriter) const final {
227 Value offset = LLVM::ConstantOp::create(
228 rewriter, op.getLoc(), rewriter.getI32Type(), op.getOffsetAttr());
229 Value ptr = LLVM::GEPOp::create(
230 rewriter, op.getLoc(), adaptor.getStorage().getType(),
231 rewriter.getI8Type(), adaptor.getStorage(), offset);
232 rewriter.replaceOp(op, ptr);
233 return success();
234 }
235};
236
237struct MemoryAccess {
238 Value ptr;
239 Value withinBounds;
240};
241
242static MemoryAccess prepareMemoryAccess(Location loc, Value memory,
243 Value address, MemoryType type,
244 ConversionPatternRewriter &rewriter) {
245 auto zextAddrType = rewriter.getIntegerType(
246 cast<IntegerType>(address.getType()).getWidth() + 1);
247 Value addr = LLVM::ZExtOp::create(rewriter, loc, zextAddrType, address);
248 Value addrLimit =
249 LLVM::ConstantOp::create(rewriter, loc, zextAddrType,
250 rewriter.getI32IntegerAttr(type.getNumWords()));
251 Value withinBounds = LLVM::ICmpOp::create(
252 rewriter, loc, LLVM::ICmpPredicate::ult, addr, addrLimit);
253 Value ptr = LLVM::GEPOp::create(
254 rewriter, loc, LLVM::LLVMPointerType::get(memory.getContext()),
255 rewriter.getIntegerType(type.getStride() * 8), memory, ValueRange{addr});
256 return {ptr, withinBounds};
257}
258
259struct MemoryReadOpLowering : public OpConversionPattern<arc::MemoryReadOp> {
260 using OpConversionPattern::OpConversionPattern;
261 LogicalResult
262 matchAndRewrite(arc::MemoryReadOp op, OpAdaptor adaptor,
263 ConversionPatternRewriter &rewriter) const final {
264 auto type = typeConverter->convertType(op.getType());
265 auto memoryType = cast<MemoryType>(op.getMemory().getType());
266 auto access =
267 prepareMemoryAccess(op.getLoc(), adaptor.getMemory(),
268 adaptor.getAddress(), memoryType, rewriter);
269
270 // Only attempt to read the memory if the address is within bounds,
271 // otherwise produce a zero value.
272 rewriter.replaceOpWithNewOp<scf::IfOp>(
273 op, access.withinBounds,
274 [&](auto &builder, auto loc) {
275 Value loadOp = LLVM::LoadOp::create(
276 builder, loc, memoryType.getWordType(), access.ptr);
277 scf::YieldOp::create(builder, loc, loadOp);
278 },
279 [&](auto &builder, auto loc) {
280 Value zeroValue = LLVM::ConstantOp::create(
281 builder, loc, type, builder.getI64IntegerAttr(0));
282 scf::YieldOp::create(builder, loc, zeroValue);
283 });
284 return success();
285 }
286};
287
288struct MemoryWriteOpLowering : public OpConversionPattern<arc::MemoryWriteOp> {
289 using OpConversionPattern::OpConversionPattern;
290 LogicalResult
291 matchAndRewrite(arc::MemoryWriteOp op, OpAdaptor adaptor,
292 ConversionPatternRewriter &rewriter) const final {
293 auto access = prepareMemoryAccess(
294 op.getLoc(), adaptor.getMemory(), adaptor.getAddress(),
295 cast<MemoryType>(op.getMemory().getType()), rewriter);
296 auto enable = access.withinBounds;
297 if (adaptor.getEnable())
298 enable = LLVM::AndOp::create(rewriter, op.getLoc(), adaptor.getEnable(),
299 enable);
300
301 // Only attempt to write the memory if the address is within bounds.
302 rewriter.replaceOpWithNewOp<scf::IfOp>(
303 op, enable, [&](auto &builder, auto loc) {
304 LLVM::StoreOp::create(builder, loc, adaptor.getData(), access.ptr);
305 scf::YieldOp::create(builder, loc);
306 });
307 return success();
308 }
309};
310
311/// A dummy lowering for clock gates to an AND gate.
312struct ClockGateOpLowering : public OpConversionPattern<seq::ClockGateOp> {
313 using OpConversionPattern::OpConversionPattern;
314 LogicalResult
315 matchAndRewrite(seq::ClockGateOp op, OpAdaptor adaptor,
316 ConversionPatternRewriter &rewriter) const final {
317 rewriter.replaceOpWithNewOp<LLVM::AndOp>(op, adaptor.getInput(),
318 adaptor.getEnable());
319 return success();
320 }
321};
322
323/// Lower 'seq.clock_inv x' to 'llvm.xor x true'
324struct ClockInvOpLowering : public OpConversionPattern<seq::ClockInverterOp> {
325 using OpConversionPattern::OpConversionPattern;
326 LogicalResult
327 matchAndRewrite(seq::ClockInverterOp op, OpAdaptor adaptor,
328 ConversionPatternRewriter &rewriter) const final {
329 auto constTrue = LLVM::ConstantOp::create(rewriter, op->getLoc(),
330 rewriter.getI1Type(), 1);
331 rewriter.replaceOpWithNewOp<LLVM::XOrOp>(op, adaptor.getInput(), constTrue);
332 return success();
333 }
334};
335
336struct ZeroCountOpLowering : public OpConversionPattern<arc::ZeroCountOp> {
337 using OpConversionPattern::OpConversionPattern;
338 LogicalResult
339 matchAndRewrite(arc::ZeroCountOp op, OpAdaptor adaptor,
340 ConversionPatternRewriter &rewriter) const override {
341 // Use poison when input is zero.
342 IntegerAttr isZeroPoison = rewriter.getBoolAttr(true);
343
344 if (op.getPredicate() == arc::ZeroCountPredicate::leading) {
345 rewriter.replaceOpWithNewOp<LLVM::CountLeadingZerosOp>(
346 op, adaptor.getInput().getType(), adaptor.getInput(), isZeroPoison);
347 return success();
348 }
349
350 rewriter.replaceOpWithNewOp<LLVM::CountTrailingZerosOp>(
351 op, adaptor.getInput().getType(), adaptor.getInput(), isZeroPoison);
352 return success();
353 }
354};
355
356struct SeqConstClockLowering : public OpConversionPattern<seq::ConstClockOp> {
357 using OpConversionPattern::OpConversionPattern;
358 LogicalResult
359 matchAndRewrite(seq::ConstClockOp op, OpAdaptor adaptor,
360 ConversionPatternRewriter &rewriter) const override {
361 rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(
362 op, rewriter.getI1Type(), static_cast<int64_t>(op.getValue()));
363 return success();
364 }
365};
366
367template <typename OpTy>
368struct ReplaceOpWithInputPattern : public OpConversionPattern<OpTy> {
370 using OpAdaptor = typename OpTy::Adaptor;
371 LogicalResult
372 matchAndRewrite(OpTy op, OpAdaptor adaptor,
373 ConversionPatternRewriter &rewriter) const override {
374 rewriter.replaceOp(op, adaptor.getInput());
375 return success();
376 }
377};
378
379} // namespace
380
381//===----------------------------------------------------------------------===//
382// Simulation Orchestration Lowering Patterns
383//===----------------------------------------------------------------------===//
384
385namespace {
386
387struct ModelInfoMap {
388 size_t numStateBytes;
389 llvm::DenseMap<StringRef, StateInfo> states;
390 mlir::FlatSymbolRefAttr initialFnSymbol;
391 mlir::FlatSymbolRefAttr finalFnSymbol;
392};
393
394template <typename OpTy>
395struct ModelAwarePattern : public OpConversionPattern<OpTy> {
396 ModelAwarePattern(const TypeConverter &typeConverter, MLIRContext *context,
397 llvm::DenseMap<StringRef, ModelInfoMap> &modelInfo)
398 : OpConversionPattern<OpTy>(typeConverter, context),
399 modelInfo(modelInfo) {}
400
401protected:
402 Value createPtrToPortState(ConversionPatternRewriter &rewriter, Location loc,
403 Value state, const StateInfo &port) const {
404 MLIRContext *ctx = rewriter.getContext();
405 return LLVM::GEPOp::create(rewriter, loc, LLVM::LLVMPointerType::get(ctx),
406 IntegerType::get(ctx, 8), state,
407 LLVM::GEPArg(port.offset));
408 }
409
410 llvm::DenseMap<StringRef, ModelInfoMap> &modelInfo;
411};
412
413/// Lowers SimInstantiateOp to a malloc and memset call. This pattern will
414/// mutate the global module.
415struct SimInstantiateOpLowering
416 : public ModelAwarePattern<arc::SimInstantiateOp> {
417 using ModelAwarePattern::ModelAwarePattern;
418
419 LogicalResult
420 matchAndRewrite(arc::SimInstantiateOp op, OpAdaptor adaptor,
421 ConversionPatternRewriter &rewriter) const final {
422 auto modelIt = modelInfo.find(
423 cast<SimModelInstanceType>(op.getBody().getArgument(0).getType())
424 .getModel()
425 .getValue());
426 ModelInfoMap &model = modelIt->second;
427
428 bool useRuntime = op.getRuntimeModel().has_value();
429
430 ModuleOp moduleOp = op->getParentOfType<ModuleOp>();
431 if (!moduleOp)
432 return failure();
433
434 ConversionPatternRewriter::InsertionGuard guard(rewriter);
435
436 // FIXME: like the rest of MLIR, this assumes sizeof(intptr_t) ==
437 // sizeof(size_t) on the target architecture.
438 Type convertedIndex = typeConverter->convertType(rewriter.getIndexType());
439 Location loc = op.getLoc();
440 Value allocated;
441
442 if (useRuntime) {
443 // The instance is using the runtime library
444 auto ptrTy = LLVM::LLVMPointerType::get(getContext());
445
446 Value runtimeArgs;
447 // If present, materialize the runtime argument string on the stack
448 if (op.getRuntimeArgs().has_value()) {
449 SmallVector<int8_t> argStringVec(op.getRuntimeArgsAttr().begin(),
450 op.getRuntimeArgsAttr().end());
451 argStringVec.push_back('\0');
452 auto strAttr = mlir::DenseElementsAttr::get(
453 mlir::RankedTensorType::get({(int64_t)argStringVec.size()},
454 rewriter.getI8Type()),
455 llvm::ArrayRef(argStringVec));
456
457 auto arrayCst = LLVM::ConstantOp::create(
458 rewriter, loc,
459 LLVM::LLVMArrayType::get(rewriter.getI8Type(), argStringVec.size()),
460 strAttr);
461 auto cst1 = LLVM::ConstantOp::create(rewriter, loc,
462 rewriter.getI32IntegerAttr(1));
463 runtimeArgs = LLVM::AllocaOp::create(rewriter, loc, ptrTy,
464 arrayCst.getType(), cst1);
465 LLVM::LifetimeStartOp::create(rewriter, loc, runtimeArgs);
466 LLVM::StoreOp::create(rewriter, loc, arrayCst, runtimeArgs);
467 } else {
468 runtimeArgs = LLVM::ZeroOp::create(rewriter, loc, ptrTy).getResult();
469 }
470 // Call the state allocation function
471 auto rtModelPtr = LLVM::AddressOfOp::create(rewriter, loc, ptrTy,
472 op.getRuntimeModelAttr())
473 .getResult();
474 allocated =
475 LLVM::CallOp::create(rewriter, loc, {ptrTy},
476 runtime::APICallbacks::symNameAllocInstance,
477 {rtModelPtr, runtimeArgs})
478 .getResult();
479
480 if (op.getRuntimeArgs().has_value())
481 LLVM::LifetimeEndOp::create(rewriter, loc, runtimeArgs);
482
483 } else {
484 // The instance is not using the runtime library
485 FailureOr<LLVM::LLVMFuncOp> mallocFunc =
486 LLVM::lookupOrCreateMallocFn(rewriter, moduleOp, convertedIndex);
487 if (failed(mallocFunc))
488 return mallocFunc;
489
490 Value numStateBytes = LLVM::ConstantOp::create(
491 rewriter, loc, convertedIndex, model.numStateBytes);
492 allocated = LLVM::CallOp::create(rewriter, loc, mallocFunc.value(),
493 ValueRange{numStateBytes})
494 .getResult();
495 Value zero =
496 LLVM::ConstantOp::create(rewriter, loc, rewriter.getI8Type(), 0);
497 LLVM::MemsetOp::create(rewriter, loc, allocated, zero, numStateBytes,
498 false);
499 }
500
501 // Call the model's 'initial' function if present.
502 if (model.initialFnSymbol) {
503 auto initialFnType = LLVM::LLVMFunctionType::get(
504 LLVM::LLVMVoidType::get(op.getContext()),
505 {LLVM::LLVMPointerType::get(op.getContext())});
506 LLVM::CallOp::create(rewriter, loc, initialFnType, model.initialFnSymbol,
507 ValueRange{allocated});
508 }
509
510 // Call the runtime's 'onInitialized' function if present.
511 if (useRuntime)
512 LLVM::CallOp::create(rewriter, loc, TypeRange{},
513 runtime::APICallbacks::symNameOnInitialized,
514 {allocated});
515
516 // Execute the body.
517 rewriter.inlineBlockBefore(&adaptor.getBody().getBlocks().front(), op,
518 {allocated});
519
520 // Call the model's 'final' function if present.
521 if (model.finalFnSymbol) {
522 auto finalFnType = LLVM::LLVMFunctionType::get(
523 LLVM::LLVMVoidType::get(op.getContext()),
524 {LLVM::LLVMPointerType::get(op.getContext())});
525 LLVM::CallOp::create(rewriter, loc, finalFnType, model.finalFnSymbol,
526 ValueRange{allocated});
527 }
528
529 if (useRuntime) {
530 LLVM::CallOp::create(rewriter, loc, TypeRange{},
531 runtime::APICallbacks::symNameDeleteInstance,
532 {allocated});
533 } else {
534 FailureOr<LLVM::LLVMFuncOp> freeFunc =
535 LLVM::lookupOrCreateFreeFn(rewriter, moduleOp);
536 if (failed(freeFunc))
537 return freeFunc;
538
539 LLVM::CallOp::create(rewriter, loc, freeFunc.value(),
540 ValueRange{allocated});
541 }
542
543 rewriter.eraseOp(op);
544 return success();
545 }
546};
547
548struct SimSetInputOpLowering : public ModelAwarePattern<arc::SimSetInputOp> {
549 using ModelAwarePattern::ModelAwarePattern;
550
551 LogicalResult
552 matchAndRewrite(arc::SimSetInputOp op, OpAdaptor adaptor,
553 ConversionPatternRewriter &rewriter) const final {
554 auto modelIt =
555 modelInfo.find(cast<SimModelInstanceType>(op.getInstance().getType())
556 .getModel()
557 .getValue());
558 ModelInfoMap &model = modelIt->second;
559
560 auto portIt = model.states.find(op.getInput());
561 if (portIt == model.states.end()) {
562 // If the port is not found in the state, it means the model does not
563 // actually use it. Thus this operation is a no-op.
564 rewriter.eraseOp(op);
565 return success();
566 }
567
568 StateInfo &port = portIt->second;
569 Value statePtr = createPtrToPortState(rewriter, op.getLoc(),
570 adaptor.getInstance(), port);
571 rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getValue(),
572 statePtr);
573
574 return success();
575 }
576};
577
578struct SimGetPortOpLowering : public ModelAwarePattern<arc::SimGetPortOp> {
579 using ModelAwarePattern::ModelAwarePattern;
580
581 LogicalResult
582 matchAndRewrite(arc::SimGetPortOp op, OpAdaptor adaptor,
583 ConversionPatternRewriter &rewriter) const final {
584 auto modelIt =
585 modelInfo.find(cast<SimModelInstanceType>(op.getInstance().getType())
586 .getModel()
587 .getValue());
588 ModelInfoMap &model = modelIt->second;
589
590 auto type = typeConverter->convertType(op.getValue().getType());
591 if (!type)
592 return failure();
593 auto portIt = model.states.find(op.getPort());
594 if (portIt == model.states.end()) {
595 // If the port is not found in the state, it means the model does not
596 // actually set it. Thus this operation returns 0.
597 rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(op, type, 0);
598 return success();
599 }
600
601 StateInfo &port = portIt->second;
602 Value statePtr = createPtrToPortState(rewriter, op.getLoc(),
603 adaptor.getInstance(), port);
604 rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, type, statePtr);
605
606 return success();
607 }
608};
609
610struct SimStepOpLowering : public ModelAwarePattern<arc::SimStepOp> {
611 using ModelAwarePattern::ModelAwarePattern;
612
613 LogicalResult
614 matchAndRewrite(arc::SimStepOp op, OpAdaptor adaptor,
615 ConversionPatternRewriter &rewriter) const final {
616 StringRef modelName = cast<SimModelInstanceType>(op.getInstance().getType())
617 .getModel()
618 .getValue();
619
620 StringAttr evalFunc =
621 rewriter.getStringAttr(evalSymbolFromModelName(modelName));
622 rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, mlir::TypeRange(), evalFunc,
623 adaptor.getInstance());
624
625 return success();
626 }
627};
628
629// Loads the simulation time (i64 femtoseconds) from byte offset 0 in the
630// model instance's state storage.
631struct SimGetTimeOpLowering : public OpConversionPattern<arc::SimGetTimeOp> {
632 using OpConversionPattern::OpConversionPattern;
633
634 LogicalResult
635 matchAndRewrite(arc::SimGetTimeOp op, OpAdaptor adaptor,
636 ConversionPatternRewriter &rewriter) const final {
637 // Time is stored at offset 0 in the instance storage.
638 rewriter.replaceOpWithNewOp<LLVM::LoadOp>(op, rewriter.getI64Type(),
639 adaptor.getInstance());
640 return success();
641 }
642};
643
644// Stores the simulation time (i64 femtoseconds) to byte offset 0 in the
645// model instance's state storage.
646struct SimSetTimeOpLowering : public OpConversionPattern<arc::SimSetTimeOp> {
647 using OpConversionPattern::OpConversionPattern;
648
649 LogicalResult
650 matchAndRewrite(arc::SimSetTimeOp op, OpAdaptor adaptor,
651 ConversionPatternRewriter &rewriter) const final {
652 // Time is stored at offset 0 in the instance storage.
653 rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getTime(),
654 adaptor.getInstance());
655 return success();
656 }
657};
658
659// Global string constants in the module.
660class StringCache {
661public:
662 Value getOrCreate(OpBuilder &b, StringRef formatStr) {
663 auto it = cache.find(formatStr);
664 if (it != cache.end()) {
665 return LLVM::AddressOfOp::create(b, b.getUnknownLoc(), it->second);
666 }
667
668 Location loc = b.getUnknownLoc();
669 LLVM::GlobalOp global;
670 {
671 OpBuilder::InsertionGuard guard(b);
672 ModuleOp m =
673 b.getInsertionBlock()->getParent()->getParentOfType<ModuleOp>();
674 b.setInsertionPointToStart(m.getBody());
675
676 SmallVector<char> strVec(formatStr.begin(), formatStr.end());
677 strVec.push_back(0);
678
679 auto name = llvm::formatv("_arc_str_{0}", cache.size()).str();
680 auto globalType = LLVM::LLVMArrayType::get(b.getI8Type(), strVec.size());
681 global = LLVM::GlobalOp::create(b, loc, globalType, /*isConstant=*/true,
682 LLVM::Linkage::Internal,
683 /*name=*/name, b.getStringAttr(strVec),
684 /*alignment=*/0);
685 }
686
687 cache[formatStr] = global;
688 return LLVM::AddressOfOp::create(b, loc, global);
689 }
690
691private:
692 llvm::StringMap<LLVM::GlobalOp> cache;
693};
694
695FailureOr<LLVM::CallOp> emitPrintfCall(OpBuilder &builder, Location loc,
696 StringCache &cache, StringRef formatStr,
697 ValueRange args) {
698 ModuleOp moduleOp =
699 builder.getInsertionBlock()->getParent()->getParentOfType<ModuleOp>();
700 // Lookup or create printf function symbol.
701 MLIRContext *ctx = builder.getContext();
702 auto printfFunc = LLVM::lookupOrCreateFn(builder, moduleOp, "printf",
703 LLVM::LLVMPointerType::get(ctx),
704 LLVM::LLVMVoidType::get(ctx), true);
705 if (failed(printfFunc))
706 return printfFunc;
707
708 Value formatStrPtr = cache.getOrCreate(builder, formatStr);
709 SmallVector<Value> argsVec(1, formatStrPtr);
710 argsVec.append(args.begin(), args.end());
711 return LLVM::CallOp::create(builder, loc, printfFunc.value(), argsVec);
712}
713
714/// Lowers SimEmitValueOp to a printf call. The integer will be printed in its
715/// entirety if it is of size up to size_t, and explicitly truncated otherwise.
716/// This pattern will mutate the global module.
717struct SimEmitValueOpLowering
718 : public OpConversionPattern<arc::SimEmitValueOp> {
719 SimEmitValueOpLowering(const TypeConverter &typeConverter,
720 MLIRContext *context, StringCache &formatStringCache)
721 : OpConversionPattern(typeConverter, context),
722 formatStringCache(formatStringCache) {}
723
724 LogicalResult
725 matchAndRewrite(arc::SimEmitValueOp op, OpAdaptor adaptor,
726 ConversionPatternRewriter &rewriter) const final {
727 auto valueType = dyn_cast<IntegerType>(adaptor.getValue().getType());
728 if (!valueType)
729 return failure();
730
731 Location loc = op.getLoc();
732
733 ModuleOp moduleOp = op->getParentOfType<ModuleOp>();
734 if (!moduleOp)
735 return failure();
736
737 SmallVector<Value> printfVariadicArgs;
738 SmallString<16> printfFormatStr;
739 int remainingBits = valueType.getWidth();
740 Value value = adaptor.getValue();
741
742 // Assumes the target platform uses 64bit for long long ints (%llx
743 // formatter).
744 constexpr llvm::StringRef intFormatter = "llx";
745 auto intType = IntegerType::get(getContext(), 64);
746 Value shiftValue = LLVM::ConstantOp::create(
747 rewriter, loc, rewriter.getIntegerAttr(valueType, intType.getWidth()));
748
749 if (valueType.getWidth() < intType.getWidth()) {
750 int width = llvm::divideCeil(valueType.getWidth(), 4);
751 printfFormatStr = llvm::formatv("%0{0}{1}", width, intFormatter);
752 printfVariadicArgs.push_back(
753 LLVM::ZExtOp::create(rewriter, loc, intType, value));
754 } else {
755 // Process the value in 64 bit chunks, starting from the least significant
756 // bits. Since we append chunks in low-to-high order, we reverse the
757 // vector to print them in the correct high-to-low order.
758 int otherChunkWidth = intType.getWidth() / 4;
759 int firstChunkWidth =
760 llvm::divideCeil(valueType.getWidth() % intType.getWidth(), 4);
761 if (firstChunkWidth == 0) { // print the full 64-bit hex or a subset.
762 firstChunkWidth = otherChunkWidth;
763 }
764
765 std::string firstChunkFormat =
766 llvm::formatv("%0{0}{1}", firstChunkWidth, intFormatter);
767 std::string otherChunkFormat =
768 llvm::formatv("%0{0}{1}", otherChunkWidth, intFormatter);
769
770 for (int i = 0; remainingBits > 0; ++i) {
771 // Append 64-bit chunks to the printf arguments, in low-to-high
772 // order. The integer is printed in hex format with zero padding.
773 printfVariadicArgs.push_back(
774 LLVM::TruncOp::create(rewriter, loc, intType, value));
775
776 // Zero-padded format specifier for fixed width, e.g. %01llx for 4 bits.
777 printfFormatStr.append(i == 0 ? firstChunkFormat : otherChunkFormat);
778
779 value =
780 LLVM::LShrOp::create(rewriter, loc, value, shiftValue).getResult();
781 remainingBits -= intType.getWidth();
782 }
783 }
784
785 std::reverse(printfVariadicArgs.begin(), printfVariadicArgs.end());
786
787 SmallString<16> formatStr = adaptor.getValueName();
788 formatStr.append(" = ");
789 formatStr.append(printfFormatStr);
790 formatStr.append("\n");
791
792 auto callOp = emitPrintfCall(rewriter, op->getLoc(), formatStringCache,
793 formatStr, printfVariadicArgs);
794 if (failed(callOp))
795 return failure();
796 rewriter.replaceOp(op, *callOp);
797
798 return success();
799 }
800
801 StringCache &formatStringCache;
802};
803
804//===----------------------------------------------------------------------===//
805// `sim` dialect lowerings
806//===----------------------------------------------------------------------===//
807
808// Helper struct to hold the format string and arguments for arcRuntimeFormat.
809struct FormatInfo {
810 SmallVector<FmtDescriptor> descriptors;
811 SmallVector<Value> args;
812};
813
814// Copies the given integer value into an alloca, returning a pointer to it.
815//
816// The alloca is rounded up to a 64-bit boundary and is written as little-endian
817// words of size 64-bits, to be compatible with the constructor of APInt.
818static Value reg2mem(ConversionPatternRewriter &rewriter, Location loc,
819 Value value) {
820 // Round up the type size to a 64-bit boundary.
821 int64_t origBitwidth = cast<IntegerType>(value.getType()).getWidth();
822 int64_t bitwidth = llvm::divideCeil(origBitwidth, 64) * 64;
823 int64_t numWords = bitwidth / 64;
824
825 // Create an alloca for the rounded up type.
826 LLVM::ConstantOp alloca_size =
827 LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), numWords);
828 auto ptrType = LLVM::LLVMPointerType::get(rewriter.getContext());
829 auto allocaOp = LLVM::AllocaOp::create(rewriter, loc, ptrType,
830 rewriter.getI64Type(), alloca_size);
831 LLVM::LifetimeStartOp::create(rewriter, loc, allocaOp);
832
833 // Copy `value` into the alloca, 64-bits at a time from the least significant
834 // bits first.
835 for (int64_t wordIdx = 0; wordIdx < numWords; ++wordIdx) {
836 Value cst = LLVM::ConstantOp::create(
837 rewriter, loc, rewriter.getIntegerType(origBitwidth), wordIdx * 64);
838 Value v = LLVM::LShrOp::create(rewriter, loc, value, cst);
839 if (origBitwidth > 64) {
840 v = LLVM::TruncOp::create(rewriter, loc, rewriter.getI64Type(), v);
841 } else if (origBitwidth < 64) {
842 v = LLVM::ZExtOp::create(rewriter, loc, rewriter.getI64Type(), v);
843 }
844 Value gep = LLVM::GEPOp::create(rewriter, loc, ptrType,
845 rewriter.getI64Type(), allocaOp, {wordIdx});
846 LLVM::StoreOp::create(rewriter, loc, v, gep);
847 }
848
849 return allocaOp;
850}
851
852// Statically folds a value of type sim::FormatStringType to a FormatInfo.
853static FailureOr<FormatInfo>
854foldFormatString(ConversionPatternRewriter &rewriter, Value fstringValue,
855 StringCache &cache) {
856 Operation *op = fstringValue.getDefiningOp();
857 return llvm::TypeSwitch<Operation *, FailureOr<FormatInfo>>(op)
858 .Case<sim::FormatCharOp>(
859 [&](sim::FormatCharOp op) -> FailureOr<FormatInfo> {
860 FmtDescriptor d = FmtDescriptor::createChar();
861 return FormatInfo{{d}, {op.getValue()}};
862 })
863 .Case<sim::FormatDecOp>([&](sim::FormatDecOp op)
864 -> FailureOr<FormatInfo> {
865 FmtDescriptor d = FmtDescriptor::createInt(
866 op.getValue().getType().getWidth(), 10, op.getIsLeftAligned(),
867 op.getSpecifierWidth().value_or(-1), false, op.getIsSigned());
868 return FormatInfo{{d}, {reg2mem(rewriter, op.getLoc(), op.getValue())}};
869 })
870 .Case<sim::FormatHexOp>([&](sim::FormatHexOp op)
871 -> FailureOr<FormatInfo> {
872 FmtDescriptor d = FmtDescriptor::createInt(
873 op.getValue().getType().getWidth(), 16, op.getIsLeftAligned(),
874 op.getSpecifierWidth().value_or(-1), op.getIsHexUppercase(), false);
875 return FormatInfo{{d}, {reg2mem(rewriter, op.getLoc(), op.getValue())}};
876 })
877 .Case<sim::FormatOctOp>([&](sim::FormatOctOp op)
878 -> FailureOr<FormatInfo> {
879 FmtDescriptor d = FmtDescriptor::createInt(
880 op.getValue().getType().getWidth(), 8, op.getIsLeftAligned(),
881 op.getSpecifierWidth().value_or(-1), false, false);
882 return FormatInfo{{d}, {reg2mem(rewriter, op.getLoc(), op.getValue())}};
883 })
884 .Case<sim::FormatLiteralOp>(
885 [&](sim::FormatLiteralOp op) -> FailureOr<FormatInfo> {
886 if (op.getLiteral().size() < 8 &&
887 op.getLiteral().find('\0') == StringRef::npos) {
888 // We can use the small string optimization.
889 FmtDescriptor d =
890 FmtDescriptor::createSmallLiteral(op.getLiteral());
891 return FormatInfo{{d}, {}};
892 }
893 FmtDescriptor d =
894 FmtDescriptor::createLiteral(op.getLiteral().size());
895 Value value = cache.getOrCreate(rewriter, op.getLiteral());
896 return FormatInfo{{d}, {value}};
897 })
898 .Case<sim::FormatStringConcatOp>(
899 [&](sim::FormatStringConcatOp op) -> FailureOr<FormatInfo> {
900 auto fmt = foldFormatString(rewriter, op.getInputs()[0], cache);
901 if (failed(fmt))
902 return failure();
903 for (auto input : op.getInputs().drop_front()) {
904 auto next = foldFormatString(rewriter, input, cache);
905 if (failed(next))
906 return failure();
907 fmt->descriptors.append(next->descriptors);
908 fmt->args.append(next->args);
909 }
910 return fmt;
911 })
912 .Default(
913 [](Operation *op) -> FailureOr<FormatInfo> { return failure(); });
914}
915
916FailureOr<LLVM::CallOp> emitFmtCall(OpBuilder &builder, Location loc,
917 StringCache &stringCache,
918 ArrayRef<FmtDescriptor> descriptors,
919 ValueRange args) {
920 ModuleOp moduleOp =
921 builder.getInsertionBlock()->getParent()->getParentOfType<ModuleOp>();
922 // Lookup or create the arcRuntimeFormat function symbol.
923 MLIRContext *ctx = builder.getContext();
924 auto func = LLVM::lookupOrCreateFn(
925 builder, moduleOp, runtime::APICallbacks::symNameFormat,
926 LLVM::LLVMPointerType::get(ctx), LLVM::LLVMVoidType::get(ctx), true);
927 if (failed(func))
928 return func;
929
930 StringRef rawDescriptors(reinterpret_cast<const char *>(descriptors.data()),
931 descriptors.size() * sizeof(FmtDescriptor));
932 Value fmtPtr = stringCache.getOrCreate(builder, rawDescriptors);
933
934 SmallVector<Value> argsVec(1, fmtPtr);
935 argsVec.append(args.begin(), args.end());
936 auto result = LLVM::CallOp::create(builder, loc, func.value(), argsVec);
937
938 for (Value arg : args) {
939 Operation *definingOp = arg.getDefiningOp();
940 if (auto alloca = dyn_cast_if_present<LLVM::AllocaOp>(definingOp)) {
941 LLVM::LifetimeEndOp::create(builder, loc, arg);
942 }
943 }
944
945 return result;
946}
947
948struct SimPrintFormattedProcOpLowering
949 : public OpConversionPattern<sim::PrintFormattedProcOp> {
950 SimPrintFormattedProcOpLowering(const TypeConverter &typeConverter,
951 MLIRContext *context,
952 StringCache &stringCache)
953 : OpConversionPattern<sim::PrintFormattedProcOp>(typeConverter, context),
954 stringCache(stringCache) {}
955
956 LogicalResult
957 matchAndRewrite(sim::PrintFormattedProcOp op, OpAdaptor adaptor,
958 ConversionPatternRewriter &rewriter) const override {
959 auto formatInfo = foldFormatString(rewriter, op.getInput(), stringCache);
960 if (failed(formatInfo))
961 return rewriter.notifyMatchFailure(op, "unsupported format string");
962
963 // Add the end descriptor.
964 formatInfo->descriptors.push_back(FmtDescriptor());
965
966 auto result = emitFmtCall(rewriter, op.getLoc(), stringCache,
967 formatInfo->descriptors, formatInfo->args);
968 if (failed(result))
969 return failure();
970 rewriter.replaceOp(op, result.value());
971
972 return success();
973 }
974
975 StringCache &stringCache;
976};
977
978} // namespace
979
980static LogicalResult convert(arc::ExecuteOp op, arc::ExecuteOp::Adaptor adaptor,
981 ConversionPatternRewriter &rewriter,
982 const TypeConverter &converter) {
983 // Convert the argument types in the body blocks.
984 if (failed(rewriter.convertRegionTypes(&op.getBody(), converter)))
985 return failure();
986
987 // Split the block at the current insertion point such that we can branch into
988 // the `arc.execute` body region, and have `arc.output` branch back to the
989 // point after the `arc.execute`.
990 auto *blockBefore = rewriter.getInsertionBlock();
991 auto *blockAfter =
992 rewriter.splitBlock(blockBefore, rewriter.getInsertionPoint());
993
994 // Branch to the entry block.
995 rewriter.setInsertionPointToEnd(blockBefore);
996 mlir::cf::BranchOp::create(rewriter, op.getLoc(), &op.getBody().front(),
997 adaptor.getInputs());
998
999 // Make all `arc.output` terminators branch to the block after the
1000 // `arc.execute` op.
1001 for (auto &block : op.getBody()) {
1002 auto outputOp = dyn_cast<arc::OutputOp>(block.getTerminator());
1003 if (!outputOp)
1004 continue;
1005 rewriter.setInsertionPointToEnd(&block);
1006 rewriter.replaceOpWithNewOp<mlir::cf::BranchOp>(outputOp, blockAfter,
1007 outputOp.getOperands());
1008 }
1009
1010 // Inline the body region between the before and after blocks.
1011 rewriter.inlineRegionBefore(op.getBody(), blockAfter);
1012
1013 // Add arguments to the block after the `arc.execute`, replace the op's
1014 // results with the arguments, then perform block signature conversion.
1015 SmallVector<Value> args;
1016 args.reserve(op.getNumResults());
1017 for (auto result : op.getResults())
1018 args.push_back(blockAfter->addArgument(result.getType(), result.getLoc()));
1019 rewriter.replaceOp(op, args);
1020 auto conversion = converter.convertBlockSignature(blockAfter);
1021 if (!conversion)
1022 return failure();
1023 rewriter.applySignatureConversion(blockAfter, *conversion, &converter);
1024 return success();
1025}
1026
1027//===----------------------------------------------------------------------===//
1028// Runtime Implementation
1029//===----------------------------------------------------------------------===//
1030
1031template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
1032static LLVM::GlobalOp
1033buildGlobalConstantIntArray(OpBuilder &builder, Location loc, Twine symName,
1034 SmallVectorImpl<T> &data,
1035 unsigned alignment = alignof(T)) {
1036 auto intType = builder.getIntegerType(8 * sizeof(T));
1037 Attribute denseAttr = mlir::DenseElementsAttr::get(
1038 mlir::RankedTensorType::get({(int64_t)data.size()}, intType),
1039 llvm::ArrayRef(data));
1040 auto globalOp = LLVM::GlobalOp::create(
1041 builder, loc, LLVM::LLVMArrayType::get(intType, data.size()),
1042 /*isConstant=*/true, LLVM::Linkage::Internal,
1043 builder.getStringAttr(symName), denseAttr);
1044 globalOp.setAlignmentAttr(builder.getI64IntegerAttr(alignment));
1045 return globalOp;
1046}
1047
1048// Construct a raw constant byte array from a vector of struct values
1049template <typename T>
1050static LLVM::GlobalOp
1051buildGlobalConstantRuntimeStructArray(OpBuilder &builder, Location loc,
1052 Twine symName,
1053 SmallVectorImpl<T> &array) {
1054 assert(!array.empty());
1055 static_assert(std::is_standard_layout<T>(),
1056 "Runtime struct must have standard layout");
1057 int64_t numBytes = sizeof(T) * array.size();
1058 Attribute denseAttr = mlir::DenseElementsAttr::get(
1059 mlir::RankedTensorType::get({numBytes}, builder.getI8Type()),
1060 llvm::ArrayRef(reinterpret_cast<uint8_t *>(array.data()), numBytes));
1061 auto globalOp = LLVM::GlobalOp::create(
1062 builder, loc, LLVM::LLVMArrayType::get(builder.getI8Type(), numBytes),
1063 /*isConstant=*/true, LLVM::Linkage::Internal,
1064 builder.getStringAttr(symName), denseAttr, alignof(T));
1065 return globalOp;
1066}
1067
1069 : public OpConversionPattern<arc::RuntimeModelOp> {
1070 using OpConversionPattern::OpConversionPattern;
1071
1072 static constexpr uint64_t runtimeApiVersion = ARC_RUNTIME_API_VERSION;
1073
1074 // Build the constant ArcModelTraceInfo struct and its members
1075 LLVM::GlobalOp
1076 buildTraceInfoStruct(arc::RuntimeModelOp &op,
1077 ConversionPatternRewriter &rewriter) const {
1078 if (!op.getTraceTaps().has_value() || op.getTraceTaps()->empty())
1079 return {};
1080 // Construct the array of tap names/aliases
1081 SmallVector<char> namesArray;
1082 SmallVector<ArcTraceTap> tapArray;
1083 tapArray.reserve(op.getTraceTaps()->size());
1084 for (auto attr : op.getTraceTapsAttr()) {
1085 auto tap = cast<TraceTapAttr>(attr);
1086 assert(!tap.getNames().empty() &&
1087 "Expected trace tap to have at least one name");
1088 for (auto alias : tap.getNames()) {
1089 auto aliasStr = cast<StringAttr>(alias);
1090 namesArray.append(aliasStr.begin(), aliasStr.end());
1091 namesArray.push_back('\0');
1092 }
1093 ArcTraceTap tapStruct;
1094 tapStruct.stateOffset = tap.getStateOffset();
1095 tapStruct.nameOffset = namesArray.size() - 1;
1096 tapStruct.typeBits = tap.getSigType().getValue().getIntOrFloatBitWidth();
1097 tapStruct.reserved = 0;
1098 tapArray.emplace_back(tapStruct);
1099 }
1100 auto ptrTy = LLVM::LLVMPointerType::get(getContext());
1101 auto namesGlobal = buildGlobalConstantIntArray(
1102 rewriter, op.getLoc(), "_arc_tap_names_" + op.getName(), namesArray);
1103 auto traceTapsArrayGlobal = buildGlobalConstantRuntimeStructArray(
1104 rewriter, op.getLoc(), "_arc_trace_taps_" + op.getName(), tapArray);
1105
1106 //
1107 // struct ArcModelTraceInfo {
1108 // uint64_t numTraceTaps;
1109 // struct ArcTraceTap *traceTaps;
1110 // const char *traceTapNames;
1111 // uint64_t traceBufferCapacity;
1112 // };
1113 //
1114 auto traceInfoStructType = LLVM::LLVMStructType::getLiteral(
1115 getContext(),
1116 {rewriter.getI64Type(), ptrTy, ptrTy, rewriter.getI64Type()});
1117 static_assert(sizeof(ArcModelTraceInfo) == 32 &&
1118 "Unexpected size of ArcModelTraceInfo struct");
1119
1120 auto globalSymName =
1121 rewriter.getStringAttr("_arc_trace_info_" + op.getName());
1122 auto traceInfoGlobalOp = LLVM::GlobalOp::create(
1123 rewriter, op.getLoc(), traceInfoStructType,
1124 /*isConstant=*/false, LLVM::Linkage::Internal, globalSymName,
1125 Attribute{}, alignof(ArcModelTraceInfo));
1126 OpBuilder::InsertionGuard g(rewriter);
1127
1128 // Struct Initializer
1129 Region &initRegion = traceInfoGlobalOp.getInitializerRegion();
1130 Block *initBlock = rewriter.createBlock(&initRegion);
1131 rewriter.setInsertionPointToStart(initBlock);
1132
1133 auto numTraceTapsCst = LLVM::ConstantOp::create(
1134 rewriter, op.getLoc(), rewriter.getI64IntegerAttr(tapArray.size()));
1135 auto traceTapArrayAddr =
1136 LLVM::AddressOfOp::create(rewriter, op.getLoc(), traceTapsArrayGlobal);
1137 auto tapNameArrayAddr =
1138 LLVM::AddressOfOp::create(rewriter, op.getLoc(), namesGlobal);
1139 auto bufferCapacityCst = LLVM::ConstantOp::create(
1140 rewriter, op.getLoc(),
1141 rewriter.getI64IntegerAttr(runtime::defaultTraceBufferCapacity));
1142
1143 Value initStruct =
1144 LLVM::PoisonOp::create(rewriter, op.getLoc(), traceInfoStructType);
1145
1146 // Field: uint64_t numTraceTaps
1147 initStruct =
1148 LLVM::InsertValueOp::create(rewriter, op.getLoc(), initStruct,
1149 numTraceTapsCst, ArrayRef<int64_t>{0});
1150 static_assert(offsetof(ArcModelTraceInfo, numTraceTaps) == 0,
1151 "Unexpected offset of field numTraceTaps");
1152 // Field: struct ArcTraceTap *traceTaps
1153 initStruct =
1154 LLVM::InsertValueOp::create(rewriter, op.getLoc(), initStruct,
1155 traceTapArrayAddr, ArrayRef<int64_t>{1});
1156 static_assert(offsetof(ArcModelTraceInfo, traceTaps) == 8,
1157 "Unexpected offset of field traceTaps");
1158 // Field: const char *traceTapNames
1159 initStruct =
1160 LLVM::InsertValueOp::create(rewriter, op.getLoc(), initStruct,
1161 tapNameArrayAddr, ArrayRef<int64_t>{2});
1162 static_assert(offsetof(ArcModelTraceInfo, traceTapNames) == 16,
1163 "Unexpected offset of field traceTapNames");
1164 // Field: uint64_t traceBufferCapacity
1165 initStruct =
1166 LLVM::InsertValueOp::create(rewriter, op.getLoc(), initStruct,
1167 bufferCapacityCst, ArrayRef<int64_t>{3});
1168 static_assert(offsetof(ArcModelTraceInfo, traceBufferCapacity) == 24,
1169 "Unexpected offset of field traceBufferCapacity");
1170 LLVM::ReturnOp::create(rewriter, op.getLoc(), initStruct);
1171
1172 return traceInfoGlobalOp;
1173 }
1174
1175 // Create a global LLVM struct containing the RuntimeModel metadata
1176 LogicalResult
1177 matchAndRewrite(arc::RuntimeModelOp op, OpAdaptor adaptor,
1178 ConversionPatternRewriter &rewriter) const final {
1179
1180 auto ptrTy = LLVM::LLVMPointerType::get(getContext());
1181 auto modelInfoStructType = LLVM::LLVMStructType::getLiteral(
1182 getContext(),
1183 {rewriter.getI64Type(), rewriter.getI64Type(), ptrTy, ptrTy});
1184 static_assert(sizeof(ArcRuntimeModelInfo) == 32 &&
1185 "Unexpected size of ArcRuntimeModelInfo struct");
1186
1187 rewriter.setInsertionPoint(op);
1188 auto traceInfoGlobal = buildTraceInfoStruct(op, rewriter);
1189
1190 // Construct the Model Name String GlobalOp
1191 SmallVector<char, 16> modNameArray(op.getName().begin(),
1192 op.getName().end());
1193 modNameArray.push_back('\0');
1194 auto nameGlobalType =
1195 LLVM::LLVMArrayType::get(rewriter.getI8Type(), modNameArray.size());
1196 auto globalSymName =
1197 rewriter.getStringAttr("_arc_mod_name_" + op.getName());
1198 auto nameGlobal = LLVM::GlobalOp::create(
1199 rewriter, op.getLoc(), nameGlobalType, /*isConstant=*/true,
1200 LLVM::Linkage::Internal,
1201 /*name=*/globalSymName, rewriter.getStringAttr(modNameArray),
1202 /*alignment=*/0);
1203
1204 // Construct the Model Info Struct GlobalOp
1205 // Note: The struct is supposed to be constant at runtime, but contains the
1206 // relocatable address of another symbol, so it should not be placed in the
1207 // "rodata" section.
1208 auto modInfoGlobalOp =
1209 LLVM::GlobalOp::create(rewriter, op.getLoc(), modelInfoStructType,
1210 /*isConstant=*/false, LLVM::Linkage::External,
1211 op.getSymName(), Attribute{});
1212
1213 // Struct Initializer
1214 Region &initRegion = modInfoGlobalOp.getInitializerRegion();
1215 Block *initBlock = rewriter.createBlock(&initRegion);
1216 rewriter.setInsertionPointToStart(initBlock);
1217 auto apiVersionCst = LLVM::ConstantOp::create(
1218 rewriter, op.getLoc(), rewriter.getI64IntegerAttr(runtimeApiVersion));
1219 auto numStateBytesCst = LLVM::ConstantOp::create(rewriter, op.getLoc(),
1220 op.getNumStateBytesAttr());
1221 auto nameAddr =
1222 LLVM::AddressOfOp::create(rewriter, op.getLoc(), nameGlobal);
1223 Value traceInfoPtr;
1224 if (traceInfoGlobal)
1225 traceInfoPtr =
1226 LLVM::AddressOfOp::create(rewriter, op.getLoc(), traceInfoGlobal);
1227 else
1228 traceInfoPtr = LLVM::ZeroOp::create(rewriter, op.getLoc(), ptrTy);
1229
1230 Value initStruct =
1231 LLVM::PoisonOp::create(rewriter, op.getLoc(), modelInfoStructType);
1232
1233 // Field: uint64_t apiVersion
1234 initStruct = LLVM::InsertValueOp::create(
1235 rewriter, op.getLoc(), initStruct, apiVersionCst, ArrayRef<int64_t>{0});
1236 static_assert(offsetof(ArcRuntimeModelInfo, apiVersion) == 0,
1237 "Unexpected offset of field apiVersion");
1238 // Field: uint64_t numStateBytes
1239 initStruct =
1240 LLVM::InsertValueOp::create(rewriter, op.getLoc(), initStruct,
1241 numStateBytesCst, ArrayRef<int64_t>{1});
1242 static_assert(offsetof(ArcRuntimeModelInfo, numStateBytes) == 8,
1243 "Unexpected offset of field numStateBytes");
1244 // Field: const char *modelName
1245 initStruct = LLVM::InsertValueOp::create(rewriter, op.getLoc(), initStruct,
1246 nameAddr, ArrayRef<int64_t>{2});
1247 static_assert(offsetof(ArcRuntimeModelInfo, modelName) == 16,
1248 "Unexpected offset of field modelName");
1249 // Field: struct ArcModelTraceInfo *traceInfo
1250 initStruct = LLVM::InsertValueOp::create(
1251 rewriter, op.getLoc(), initStruct, traceInfoPtr, ArrayRef<int64_t>{3});
1252 static_assert(offsetof(ArcRuntimeModelInfo, traceInfo) == 24,
1253 "Unexpected offset of field traceInfo");
1254
1255 LLVM::ReturnOp::create(rewriter, op.getLoc(), initStruct);
1256
1257 rewriter.replaceOp(op, modInfoGlobalOp);
1258 return success();
1259 }
1260};
1261
1262//===----------------------------------------------------------------------===//
1263// Pass Implementation
1264//===----------------------------------------------------------------------===//
1265
1266namespace {
1267struct LowerArcToLLVMPass
1268 : public circt::impl::LowerArcToLLVMBase<LowerArcToLLVMPass> {
1269 void runOnOperation() override;
1270};
1271} // namespace
1272
1273void LowerArcToLLVMPass::runOnOperation() {
1274 // Replace any `i0` values with an `hw.constant 0 : i0` to avoid later issues
1275 // in LLVM conversion.
1276 {
1277 DenseMap<Region *, hw::ConstantOp> zeros;
1278 getOperation().walk([&](Operation *op) {
1279 if (op->hasTrait<OpTrait::ConstantLike>())
1280 return;
1281 for (auto result : op->getResults()) {
1282 auto type = dyn_cast<IntegerType>(result.getType());
1283 if (!type || type.getWidth() != 0)
1284 continue;
1285 auto *region = op->getParentRegion();
1286 auto &zero = zeros[region];
1287 if (!zero) {
1288 auto builder = OpBuilder::atBlockBegin(&region->front());
1289 zero = hw::ConstantOp::create(builder, result.getLoc(),
1290 APInt::getZero(0));
1291 }
1292 result.replaceAllUsesWith(zero);
1293 }
1294 });
1295 }
1296
1297 // Collect the symbols in the root op such that the HW-to-LLVM lowering can
1298 // create LLVM globals with non-colliding names.
1299 Namespace globals;
1300 SymbolCache cache;
1301 cache.addDefinitions(getOperation());
1302 globals.add(cache);
1303
1304 // Setup the conversion target. Explicitly mark `scf.yield` legal since it
1305 // does not have a conversion itself, which would cause it to fail
1306 // legalization and for the conversion to abort. (It relies on its parent op's
1307 // conversion to remove it.)
1308 LLVMConversionTarget target(getContext());
1309 target.addLegalOp<mlir::ModuleOp>();
1310 target.addLegalOp<scf::YieldOp>(); // quirk of SCF dialect conversion
1311
1312 // Mark sim::Format*Op as legal. These are not converted to LLVM, but the
1313 // lowering of sim::PrintFormattedOp walks them to build up its format string.
1314 // They are all marked Pure so are removed after the conversion.
1315 target.addLegalOp<sim::FormatLiteralOp, sim::FormatDecOp, sim::FormatHexOp,
1316 sim::FormatBinOp, sim::FormatOctOp, sim::FormatCharOp,
1317 sim::FormatStringConcatOp>();
1318
1319 // Setup the arc dialect type conversion.
1320 LLVMTypeConverter converter(&getContext());
1321 converter.addConversion([&](seq::ClockType type) {
1322 return IntegerType::get(type.getContext(), 1);
1323 });
1324 converter.addConversion([&](StorageType type) {
1325 return LLVM::LLVMPointerType::get(type.getContext());
1326 });
1327 converter.addConversion([&](MemoryType type) {
1328 return LLVM::LLVMPointerType::get(type.getContext());
1329 });
1330 converter.addConversion([&](StateType type) {
1331 return LLVM::LLVMPointerType::get(type.getContext());
1332 });
1333 converter.addConversion([&](SimModelInstanceType type) {
1334 return LLVM::LLVMPointerType::get(type.getContext());
1335 });
1336 converter.addConversion([&](sim::FormatStringType type) {
1337 return LLVM::LLVMPointerType::get(type.getContext());
1338 });
1339 converter.addConversion([&](llhd::TimeType type) {
1340 // LLHD time is represented as i64 femtoseconds.
1341 return IntegerType::get(type.getContext(), 64);
1342 });
1343
1344 // Setup the conversion patterns.
1345 ConversionPatternSet patterns(&getContext(), converter);
1346
1347 // MLIR patterns.
1348 populateSCFToControlFlowConversionPatterns(patterns);
1349 populateFuncToLLVMConversionPatterns(converter, patterns);
1350 cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
1351 arith::populateArithToLLVMConversionPatterns(converter, patterns);
1352 index::populateIndexToLLVMConversionPatterns(converter, patterns);
1353 populateAnyFunctionOpInterfaceTypeConversionPattern(patterns, converter);
1354
1355 // CIRCT patterns.
1356 DenseMap<std::pair<Type, ArrayAttr>, LLVM::GlobalOp> constAggregateGlobalsMap;
1358 std::optional<HWToLLVMArraySpillCache> spillCacheOpt =
1360 {
1361 OpBuilder spillBuilder(getOperation());
1362 spillCacheOpt->spillNonHWOps(spillBuilder, converter, getOperation());
1363 }
1364 populateHWToLLVMConversionPatterns(converter, patterns, globals,
1365 constAggregateGlobalsMap, spillCacheOpt);
1366
1369
1370 // Arc patterns.
1371 // clang-format off
1372 patterns.add<
1373 AllocMemoryOpLowering,
1374 AllocStateLikeOpLowering<arc::AllocStateOp>,
1375 AllocStateLikeOpLowering<arc::RootInputOp>,
1376 AllocStateLikeOpLowering<arc::RootOutputOp>,
1377 AllocStorageOpLowering,
1378 ClockGateOpLowering,
1379 ClockInvOpLowering,
1380 CurrentTimeOpLowering,
1381 IntToTimeOpLowering,
1382 MemoryReadOpLowering,
1383 MemoryWriteOpLowering,
1384 ModelOpLowering,
1385 ReplaceOpWithInputPattern<seq::ToClockOp>,
1386 ReplaceOpWithInputPattern<seq::FromClockOp>,
1388 SeqConstClockLowering,
1389 SimGetTimeOpLowering,
1390 SimSetTimeOpLowering,
1391 StateReadOpLowering,
1392 StateWriteOpLowering,
1393 StorageGetOpLowering,
1394 TimeToIntOpLowering,
1395 ZeroCountOpLowering
1396 >(converter, &getContext());
1397 // clang-format on
1398 patterns.add<ExecuteOp>(convert);
1399
1400 StringCache stringCache;
1401 patterns.add<SimEmitValueOpLowering, SimPrintFormattedProcOpLowering>(
1402 converter, &getContext(), stringCache);
1403
1404 auto &modelInfo = getAnalysis<ModelInfoAnalysis>();
1405 llvm::DenseMap<StringRef, ModelInfoMap> modelMap(modelInfo.infoMap.size());
1406 for (auto &[_, modelInfo] : modelInfo.infoMap) {
1407 llvm::DenseMap<StringRef, StateInfo> states(modelInfo.states.size());
1408 for (StateInfo &stateInfo : modelInfo.states)
1409 states.insert({stateInfo.name, stateInfo});
1410 modelMap.insert(
1411 {modelInfo.name,
1412 ModelInfoMap{modelInfo.numStateBytes, std::move(states),
1413 modelInfo.initialFnSym, modelInfo.finalFnSym}});
1414 }
1415
1416 patterns.add<SimInstantiateOpLowering, SimSetInputOpLowering,
1417 SimGetPortOpLowering, SimStepOpLowering>(
1418 converter, &getContext(), modelMap);
1419
1420 // Apply the conversion.
1421 ConversionConfig config;
1422 config.allowPatternRollback = false;
1423 if (failed(applyFullConversion(getOperation(), target, std::move(patterns),
1424 config)))
1425 signalPassFailure();
1426}
1427
1428std::unique_ptr<OperationPass<ModuleOp>> circt::createLowerArcToLLVMPass() {
1429 return std::make_unique<LowerArcToLLVMPass>();
1430}
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static LLVM::GlobalOp buildGlobalConstantIntArray(OpBuilder &builder, Location loc, Twine symName, SmallVectorImpl< T > &data, unsigned alignment=alignof(T))
static LLVM::GlobalOp buildGlobalConstantRuntimeStructArray(OpBuilder &builder, Location loc, Twine symName, SmallVectorImpl< T > &array)
static llvm::Twine evalSymbolFromModelName(StringRef modelName)
static LogicalResult convert(arc::ExecuteOp op, arc::ExecuteOp::Adaptor adaptor, ConversionPatternRewriter &rewriter, const TypeConverter &converter)
Extension of RewritePatternSet that allows adding matchAndRewrite functions with op adaptors and Conv...
A namespace that is used to store existing names and generate new names in some scope within the IR.
Definition Namespace.h:30
void add(mlir::ModuleOp module)
Definition Namespace.h:48
void addDefinitions(mlir::Operation *top)
Populate the symbol cache with all symbol-defining operations within the 'top' operation.
Definition SymCache.cpp:23
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Definition SymCache.h:85
create(data_type, value)
Definition hw.py:433
#define ARC_RUNTIME_API_VERSION
Version of the combined public and internal API.
Definition Common.h:27
Definition arc.py:1
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
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.
void populateHWToLLVMConversionPatterns(mlir::LLVMTypeConverter &converter, RewritePatternSet &patterns, Namespace &globals, DenseMap< std::pair< Type, ArrayAttr >, mlir::LLVM::GlobalOp > &constAggregateGlobalsMap, std::optional< HWToLLVMArraySpillCache > &spillCacheOpt)
Get the HW to LLVM conversion patterns.
std::unique_ptr< OperationPass< ModuleOp > > createLowerArcToLLVMPass()
Definition hw.py:1
Definition sim.py:1
Static information for a compiled hardware model, generated by the MLIR lowering.
Definition Common.h:70
uint32_t typeBits
Bit width of the traced signal.
Definition TraceTaps.h:28
uint64_t stateOffset
Byte offset of the traced value within the model state.
Definition TraceTaps.h:23
uint64_t nameOffset
Byte offset to the null terminator of this signal's last alias in the names array.
Definition TraceTaps.h:26
uint32_t reserved
Padding and reserved for future use.
Definition TraceTaps.h:30
LLVM::GlobalOp buildTraceInfoStruct(arc::RuntimeModelOp &op, ConversionPatternRewriter &rewriter) const
static constexpr uint64_t runtimeApiVersion
LogicalResult matchAndRewrite(arc::RuntimeModelOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const final
Helper class mapping array values (HW or LLVM Dialect) to pointers to buffers containing the array va...
Definition HWToLLVM.h:47