diff --git a/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp b/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp index 7c9bb9561a7e..9163a00cbee0 100644 --- a/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp +++ b/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp @@ -29,8 +29,11 @@ #include "mlir/Pass/Pass.h" #include "mlir/Support/LogicalResult.h" #include "mlir/Transforms/GreedyPatternRewriteDriver.h" +#include "llvm/ADT/APInt.h" #include "llvm/ADT/TypeSwitch.h" +#include "llvm/BinaryFormat/MsgPackReader.h" #include "llvm/Support/LogicalResult.h" +#include "llvm/Support/NativeFormatting.h" #include "llvm/Support/raw_os_ostream.h" #include "llvm/Support/raw_ostream.h" #include @@ -38,6 +41,7 @@ #include #include +#include #include #include @@ -1301,10 +1305,19 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState, rewriter, allocOp.getLoc(), componentState.getUniqueName("mem"), memtype.getElementType().getIntOrFloatBitWidth(), sizes, addrSizes); + auto componentOp = componentState.getComponentOp(); + // Externalize memories conditionally (only in the top-level component because // Calyx compiler requires it as a well-formness check). - memoryOp->setAttr("external", - IntegerAttr::get(rewriter.getI1Type(), llvm::APInt(1, 1))); + if (componentOp->hasAttr("toplevel")) { + memoryOp->setAttr( + "external", IntegerAttr::get(rewriter.getI1Type(), llvm::APInt(1, 1))); + + } else { + memoryOp->setAttr( + "external", IntegerAttr::get(rewriter.getI1Type(), llvm::APInt(1, 0))); + } + componentState.registerMemoryInterface(allocOp.getResult(), calyx::MemoryInterface(memoryOp)); diff --git a/lib/Dialect/Calyx/Export/CalyxEmitter.cpp b/lib/Dialect/Calyx/Export/CalyxEmitter.cpp index 18a589bd11a3..147c60bed947 100644 --- a/lib/Dialect/Calyx/Export/CalyxEmitter.cpp +++ b/lib/Dialect/Calyx/Export/CalyxEmitter.cpp @@ -374,6 +374,10 @@ struct Emitter { if (!atFormat) { buffer << quote() << identifier << quote() << equals() << value; } else { + // don't emit external(0) + if (identifier == "external" && intAttr.getValue() == 0) { + return ""; + } buffer << addressSymbol() << identifier; // The only time we may omit the value is when it is a Boolean attribute // with value 1. @@ -875,6 +879,7 @@ void Emitter::emitSeqMemory(SeqMemoryOp memory) { "supported by the native Calyx compiler."); return; } + bool isRef = !memory->hasAttr("external"); indent(); if (isRef)