From fb35567340abc26f6be128078a34bafb7e765e13 Mon Sep 17 00:00:00 2001 From: Michele Scuttari Date: Sun, 6 Oct 2024 10:26:11 +0200 Subject: [PATCH 1/2] Refactor the initialization of the target machine --- .../public/marco/Frontend/FrontendActions.h | 8 +++- lib/Frontend/FrontendActions.cpp | 48 ++++++++----------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/include/public/marco/Frontend/FrontendActions.h b/include/public/marco/Frontend/FrontendActions.h index b270c583b..7cc21d219 100644 --- a/include/public/marco/Frontend/FrontendActions.h +++ b/include/public/marco/Frontend/FrontendActions.h @@ -100,6 +100,12 @@ class CodeGenAction : public ASTAction { /// Set up the LLVM's TargetMachine. bool setUpTargetMachine(); + /// Get the LLVM's TargetMachine. + llvm::TargetMachine &getTargetMachine(); + + /// Get the LLVM's TargetMachine. + const llvm::TargetMachine &getTargetMachine() const; + /// Get the data layout of the machine for which the code is being /// compiled. llvm::DataLayout getDataLayout() const; @@ -205,6 +211,7 @@ class CodeGenAction : public ASTAction { private: CodeGenActionKind action; + std::unique_ptr targetMachine; mlir::DialectRegistry mlirDialectRegistry; std::unique_ptr mlirContext; std::unique_ptr diagnosticHandler; @@ -213,7 +220,6 @@ class CodeGenAction : public ASTAction { protected: std::unique_ptr mlirModule; std::unique_ptr llvmModule; - std::unique_ptr targetMachine; }; class EmitMLIRAction : public CodeGenAction { diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index 6e1d03383..74bb18413 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/lib/Frontend/FrontendActions.cpp @@ -41,6 +41,7 @@ #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" +#include "llvm/MC/MCSubtargetInfo.h" #include "llvm/Passes/PassBuilder.h" #include "llvm/Passes/StandardInstrumentations.h" #include "llvm/Support/Path.h" @@ -419,6 +420,10 @@ bool CodeGenAction::beginSourceFilesAction() { createMLIRContext(); } + if (!setUpTargetMachine()) { + return false; + } + if (action == CodeGenActionKind::GenerateMLIR) { return generateMLIR(); } @@ -490,9 +495,18 @@ bool CodeGenAction::setUpTargetMachine() { return true; } -llvm::DataLayout CodeGenAction::getDataLayout() const { +llvm::TargetMachine &CodeGenAction::getTargetMachine() { + assert(targetMachine && "TargetMachine has not been initialized yet"); + return *targetMachine; +} + +const llvm::TargetMachine &CodeGenAction::getTargetMachine() const { assert(targetMachine && "TargetMachine has not been initialized yet"); - return targetMachine->createDataLayout(); + return *targetMachine; +} + +llvm::DataLayout CodeGenAction::getDataLayout() const { + return getTargetMachine().createDataLayout(); } void CodeGenAction::registerMLIRDialects() { @@ -688,12 +702,7 @@ bool CodeGenAction::generateMLIRLLVM() { void CodeGenAction::setMLIRModuleTargetTriple() { assert(mlirModule && "MLIR module has not been created yet"); - - if (!targetMachine) { - setUpTargetMachine(); - } - - const std::string &triple = targetMachine->getTargetTriple().str(); + const std::string &triple = getTargetMachine().getTargetTriple().str(); llvm::StringRef tripleAttrName = mlir::LLVM::LLVMDialect::getTargetTripleAttrName(); @@ -717,12 +726,7 @@ void CodeGenAction::setMLIRModuleTargetTriple() { void CodeGenAction::setMLIRModuleDataLayout() { assert(mlirModule && "MLIR module has not been created yet"); - - if (!targetMachine) { - setUpTargetMachine(); - } - - const llvm::DataLayout &dl = targetMachine->createDataLayout(); + const llvm::DataLayout &dl = getTargetMachine().createDataLayout(); llvm::StringRef dlAttrName = mlir::LLVM::LLVMDialect::getDataLayoutAttrName(); @@ -1178,12 +1182,7 @@ bool CodeGenAction::generateLLVMIR() { void CodeGenAction::setLLVMModuleTargetTriple() { assert(llvmModule && "LLVM module has not been created yet"); - - if (!targetMachine) { - setUpTargetMachine(); - } - - const std::string &triple = targetMachine->getTargetTriple().str(); + const std::string &triple = getTargetMachine().getTargetTriple().str(); if (llvmModule->getTargetTriple() != triple) { // The LLVM module already has a target triple which is different from @@ -1199,12 +1198,7 @@ void CodeGenAction::setLLVMModuleTargetTriple() { void CodeGenAction::setLLVMModuleDataLayout() { assert(llvmModule && "LLVM module has not been created yet"); - - if (!targetMachine) { - setUpTargetMachine(); - } - - const llvm::DataLayout &dataLayout = targetMachine->createDataLayout(); + const llvm::DataLayout &dataLayout = getTargetMachine().createDataLayout(); if (llvmModule->getDataLayout() != dataLayout) { // The LLVM module already has a data layout which is different from the @@ -1236,7 +1230,7 @@ void CodeGenAction::runOptimizationPipeline() { llvm::StandardInstrumentations si(getLLVMContext(), false); si.registerCallbacks(pic, &mam); - llvm::PassBuilder pb(targetMachine.get(), pto, pgoOpt, &pic); + llvm::PassBuilder pb(&getTargetMachine(), pto, pgoOpt, &pic); // Register all the basic analyses with the managers. pb.registerModuleAnalyses(mam); From 4bc03e371197d8b5864e71d09dc567e50204388e Mon Sep 17 00:00:00 2001 From: Michele Scuttari Date: Sun, 6 Oct 2024 10:26:34 +0200 Subject: [PATCH 2/2] Use cache size instead of wrong cache line size --- lib/Frontend/FrontendActions.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index 74bb18413..48b353471 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/lib/Frontend/FrontendActions.cpp @@ -1102,10 +1102,11 @@ void CodeGenAction::buildMLIRBufferDeallocationPipeline( } std::unique_ptr CodeGenAction::createMLIRLoopTilingPass() { - auto &ci = getInstance(); + const auto *subtargetInfo = getTargetMachine().getMCSubtargetInfo(); - if (auto cacheLineSize = ci.getTarget().getCPUCacheLineSize()) { - return mlir::affine::createLoopTilingPass(*cacheLineSize); + if (auto cacheSize = subtargetInfo->getCacheSize(0); + cacheSize && *cacheSize >= 1024) { + return mlir::affine::createLoopTilingPass(*cacheSize); } return mlir::affine::createLoopTilingPass();