From d581ee4a07346bb789201fb56f35d215d8939512 Mon Sep 17 00:00:00 2001 From: Jiahan Xie Date: Wed, 12 Jun 2024 18:57:53 -0400 Subject: [PATCH] add more comments for function description; use llvm join for string concat --- lib/Conversion/SCFToCalyx/SCFToCalyx.cpp | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp b/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp index 3d408542b5b5..1bf3103226f9 100644 --- a/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp +++ b/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp @@ -1086,7 +1086,8 @@ struct FuncOpConversion : public calyx::FuncOpPartialLoweringPattern { std::string funcName = "func_" + funcOp.getSymName().str(); rewriter.modifyOpInPlace(funcOp, [&]() { funcOp.setSymName(funcName); }); - /// Mark this component as the toplevel if it's the top-level function of the module. + /// Mark this component as the toplevel if it's the top-level function of + /// the module. if (compOp.getName() == loweringState().getTopLevelFunction()) compOp->setAttr("toplevel", rewriter.getUnitAttr()); @@ -1098,7 +1099,8 @@ struct FuncOpConversion : public calyx::FuncOpPartialLoweringPattern { unsigned extMemCounter = 0; for (auto arg : enumerate(funcOp.getArguments())) { if (isa(arg.value().getType())) { - std::string memName = "arg_mem" + std::to_string(extMemCounter++); + std::string memName = + llvm::join_items("_", "arg_mem", std::to_string(extMemCounter++)); rewriter.setInsertionPointToStart(compOp.getBodyBlock()); MemRefType memtype = cast(arg.value().getType()); @@ -1661,7 +1663,7 @@ class SCFToCalyxPass : public SCFToCalyxBase { } } - return createOptNewTopLevelFcn(moduleOp, topLevelFunction); + return createOptNewTopLevelFn(moduleOp, topLevelFunction); } struct LoweringPattern { @@ -1758,11 +1760,12 @@ class SCFToCalyxPass : public SCFToCalyxBase { LogicalResult partialPatternRes; std::shared_ptr loweringState = nullptr; - FuncOp createNewTopLevelFcn(ModuleOp moduleOp, std::string &baseName) { + /// Creates a new new top-level function based on `baseName`. + FuncOp createNewTopLevelFn(ModuleOp moduleOp, std::string &baseName) { std::string newName = baseName; unsigned counter = 0; while (SymbolTable::lookupSymbolIn(moduleOp, newName)) { - newName = baseName + "_" + std::to_string(++counter); + newName = llvm::join_items("_", baseName, std::to_string(++counter)); } OpBuilder builder(moduleOp.getContext()); @@ -1781,6 +1784,9 @@ class SCFToCalyxPass : public SCFToCalyxBase { return nullptr; } + /// Insert a call from the newly created top-level function/`caller` to the + /// old top-level function/`callee`; and create `memref.alloc`s inside the new + /// top-level function for arguments with `memref` types. void insertCallFromNewTopLevel(OpBuilder &builder, FuncOp caller, FuncOp callee) { if (caller.getBody().empty()) { @@ -1808,8 +1814,11 @@ class SCFToCalyxPass : public SCFToCalyxBase { memRefArgs); } - LogicalResult createOptNewTopLevelFcn(ModuleOp moduleOp, - std::string &topLevelFunction) { + /// Conditionally creates an optional new top-level function; and inserts a + /// call from the new top-level function to the old top-level function if we + /// did create one + LogicalResult createOptNewTopLevelFn(ModuleOp moduleOp, + std::string &topLevelFunction) { auto hasMemrefArguments = [](FuncOp func) { return std::any_of( func.getArguments().begin(), func.getArguments().end(), @@ -1830,7 +1839,7 @@ class SCFToCalyxPass : public SCFToCalyxBase { std::string oldName = topLevelFunction; if (hasMemrefArgsInTopLevel) { - auto newTopLevelFunc = createNewTopLevelFcn(moduleOp, topLevelFunction); + auto newTopLevelFunc = createNewTopLevelFn(moduleOp, topLevelFunction); OpBuilder builder(moduleOp.getContext()); Operation *oldTopLevelFuncOp =