Skip to content

Commit

Permalink
[aie2xclbin] Towards stdout free compilation (#1583)
Browse files Browse the repository at this point in the history
  • Loading branch information
newling authored Jun 27, 2024
1 parent d850560 commit 8b30632
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
17 changes: 13 additions & 4 deletions lib/Targets/AIETargetCDODirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ void initializeCDOGenerator(byte_ordering endianness, bool cdoDebug) {

LogicalResult generateCDOBinary(const StringRef outputPath,
const std::function<LogicalResult()> &cb) {

// TODO(newling): Get bootgen team to remove print statement in this function.
startCDOFileStream(outputPath.str().c_str());
FileHeader();
// Never generate a completely empty CDO file. If the file only contains a
Expand Down Expand Up @@ -775,6 +777,7 @@ LogicalResult AIETranslateToCDODirect(ModuleOp m, llvm::StringRef workDirPath,
bool emitUnified, bool cdoDebug,
bool aieSim, bool xaieDebug,
bool enableCores) {

auto devOps = m.getOps<DeviceOp>();
assert(llvm::range_size(devOps) == 1 &&
"only exactly 1 device op supported.");
Expand All @@ -788,10 +791,16 @@ LogicalResult AIETranslateToCDODirect(ModuleOp m, llvm::StringRef workDirPath,

AIEControl ctl(aieSim, xaieDebug, targetModel);
initializeCDOGenerator(endianness, cdoDebug);
if (emitUnified)
return generateCDOUnified(ctl, workDirPath, targetOp, aieSim, enableCores);
return generateCDOBinariesSeparately(ctl, workDirPath, targetOp, aieSim,
enableCores);

auto result = [&]() {
if (emitUnified) {
return generateCDOUnified(ctl, workDirPath, targetOp, aieSim,
enableCores);
}
return generateCDOBinariesSeparately(ctl, workDirPath, targetOp, aieSim,
enableCores);
}();
return result;
}
// Not sure why but defining this with xilinx::AIE will create a duplicate
// symbol in libAIETargets.a that then doesn't actually match the header?
Expand Down
26 changes: 22 additions & 4 deletions tools/aie2xclbin/XCLBinGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===---------------------------------------------------------------------===//

#include "XCLBinGen.h"
#include <fstream>

#include "aie/Conversion/AIEVecToLLVM/AIEVecToLLVM.h"
#include "aie/Dialect/AIE/Transforms/AIEPasses.h"
Expand Down Expand Up @@ -40,6 +41,7 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/ToolOutputFile.h"

#include <optional>
#include <regex>
#include <sstream>
#include <unordered_map>
Expand Down Expand Up @@ -212,14 +214,30 @@ int runTool(StringRef Program, ArrayRef<std::string> Args, bool Verbose,
std::optional<sys::ProcessStatistics> opt_stats(stats);
SmallVector<StringRef, 8> PArgs = {Program};
PArgs.append(Args.begin(), Args.end());
int result = sys::ExecuteAndWait(Program, PArgs, Env, {}, 0, 0, &err_msg,
nullptr, &opt_stats);
if (Verbose)

SmallVector<char> tmpPath;
auto ec = llvm::sys::fs::createTemporaryFile("run_tool", "", tmpPath);
if (ec) {
llvm::errs() << "Failed to create temporary file: " << ec.message() << "\n";
return -1;
}

// Convert tmpPath to a StringRef:
StringRef tp(tmpPath.begin(), tmpPath.size());

int result = sys::ExecuteAndWait(Program, PArgs, Env, {tp, tp, tp}, 0, 0,
&err_msg, nullptr, &opt_stats);
if (Verbose) {
llvm::outs() << (result == 0 ? "Succeeded " : "Failed ") << "in "
<< std::chrono::duration_cast<std::chrono::duration<float>>(
stats.TotalTime)
.count()
<< " code: " << result << "\n";
std::ifstream t(tp.str());
std::stringstream buffer;
buffer << t.rdbuf();
llvm::outs() << buffer.str();
}
return result;
}

Expand Down Expand Up @@ -369,13 +387,13 @@ static LogicalResult generateCDO(MLIRContext *context, ModuleOp moduleOp,
// compilation in aiecc.py... not sure we need this.
PassManager passManager(context, ModuleOp::getOperationName());
applyConfigToPassManager(TK, passManager);

passManager.addNestedPass<AIE::DeviceOp>(AIE::createAIEPathfinderPass());
passManager.addNestedPass<AIE::DeviceOp>(
AIEX::createAIEBroadcastPacketPass());
passManager.addNestedPass<AIE::DeviceOp>(
AIE::createAIERoutePacketFlowsPass());
passManager.addNestedPass<AIE::DeviceOp>(AIEX::createAIELowerMulticastPass());

if (failed(passManager.run(copy)))
return moduleOp.emitOpError(
"failed to run passes to prepare of XCLBin generation");
Expand Down
1 change: 1 addition & 0 deletions tools/aie2xclbin/aie2xclbin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ int main(int argc, char *argv[]) {
registerPassManagerCLOptions();
registerTranslationCLOptions();
cl::ParseCommandLineOptions(argc, argv);

XCLBinGenConfig TK;
TK.Verbose = Verbose;
TK.HostArch = HostArch;
Expand Down

0 comments on commit 8b30632

Please sign in to comment.