Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 675a42d

Browse files
committed
Fix format
1 parent e576a61 commit 675a42d

File tree

5 files changed

+94
-73
lines changed

5 files changed

+94
-73
lines changed

include/tc/core/polyhedral/llvm_jit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Jit {
3434
llvm::orc::RTDyldObjectLinkingLayer objectLayer_;
3535
llvm::orc::IRCompileLayer<decltype(objectLayer_), llvm::orc::SimpleCompiler>
3636
compileLayer_;
37+
3738
public:
3839
Jit();
3940

src/core/polyhedral/codegen_llvm.cc

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include "llvm/IR/Verifier.h"
2929
#include "llvm/Support/TargetSelect.h"
3030
#include "llvm/Support/raw_ostream.h"
31-
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
3231
#include "llvm/Transforms/IPO.h"
32+
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
3333
#include "llvm/Transforms/Tapir/CilkABI.h"
3434

3535
#include "Halide/Halide.h"
@@ -205,6 +205,10 @@ class IslAstExprInterpeter {
205205
}
206206
};
207207

208+
DEFINE_bool(llvm_dump_before_opt, false, "Print IR before optimization");
209+
DEFINE_bool(llvm_dump_after_opt, false, "Print IR after optimization");
210+
static constexpr int kOptLevel = 3;
211+
208212
class CodeGen_TC : public Halide::Internal::CodeGen_X86 {
209213
public:
210214
const isl::pw_multi_aff* iteratorMap_;
@@ -303,81 +307,52 @@ class CodeGen_TC : public Halide::Internal::CodeGen_X86 {
303307

304308
value = sym_get(name);
305309
}
306-
public:
307-
void optimize_module() {
308-
Halide::Internal::debug(3) << "Optimizing module\n";
309310

310-
if (Halide::Internal::debug::debug_level() >= 3) {
311-
#if LLVM_VERSION_MAJOR >= 5
312-
module->print(llvm::dbgs(), nullptr, false, true);
313-
#else
314-
module->dump();
315-
#endif
311+
public:
312+
void optimize_module() {
313+
if (FLAGS_llvm_dump_before_opt) {
314+
module->print(llvm::dbgs(), nullptr, false, true);
316315
}
317316

318-
// We override PassManager::add so that we have an opportunity to
319-
// blacklist problematic LLVM passes.
320-
class MyFunctionPassManager : public llvm::legacy::FunctionPassManager {
321-
public:
322-
MyFunctionPassManager(llvm::Module *m) : llvm::legacy::FunctionPassManager(m) {}
323-
virtual void add(llvm::Pass *p) override {
324-
Halide::Internal::debug(2) << "Adding function pass: " << p->getPassName().str() << "\n";
325-
llvm::legacy::FunctionPassManager::add(p);
326-
}
327-
};
328-
329-
class MyModulePassManager : public llvm::legacy::PassManager {
330-
public:
331-
virtual void add(llvm::Pass *p) override {
332-
Halide::Internal::debug(2) << "Adding module pass: " << p->getPassName().str() << "\n";
333-
llvm::legacy::PassManager::add(p);
334-
}
335-
};
336-
337-
MyFunctionPassManager function_pass_manager(module.get());
338-
MyModulePassManager module_pass_manager;
317+
llvm::legacy::FunctionPassManager functionPassManager(module.get());
318+
llvm::legacy::PassManager modulePassManager;
339319

340-
std::unique_ptr<llvm::TargetMachine> TM = Halide::Internal::make_target_machine(*module);
341-
module_pass_manager.add(llvm::createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() : llvm::TargetIRAnalysis()));
342-
function_pass_manager.add(llvm::createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() : llvm::TargetIRAnalysis()));
320+
std::unique_ptr<llvm::TargetMachine> targetMachine =
321+
Halide::Internal::make_target_machine(*module);
322+
modulePassManager.add(llvm::createTargetTransformInfoWrapperPass(
323+
targetMachine ? targetMachine->getTargetIRAnalysis()
324+
: llvm::TargetIRAnalysis()));
325+
functionPassManager.add(llvm::createTargetTransformInfoWrapperPass(
326+
targetMachine ? targetMachine->getTargetIRAnalysis()
327+
: llvm::TargetIRAnalysis()));
343328

344329
llvm::PassManagerBuilder b;
345-
b.OptLevel = 3;
330+
b.OptLevel = kOptLevel;
346331
b.tapirTarget = new llvm::CilkABI();
347-
#if LLVM_VERSION_MAJOR >= 5
348332
b.Inliner = llvm::createFunctionInliningPass(b.OptLevel, 0, false);
349-
#else
350-
b.Inliner = llvm::createFunctionInliningPass(b.OptLevel, 0);
351-
#endif
352333
b.LoopVectorize = true;
353334
b.SLPVectorize = true;
354335

355-
#if LLVM_VERSION_MAJOR >= 5
356-
if (TM) {
357-
TM->adjustPassManager(b);
336+
if (targetMachine) {
337+
targetMachine->adjustPassManager(b);
358338
}
359-
#endif
360339

361-
b.populateFunctionPassManager(function_pass_manager);
362-
b.populateModulePassManager(module_pass_manager);
340+
b.populateFunctionPassManager(functionPassManager);
341+
b.populateModulePassManager(modulePassManager);
363342

364343
// Run optimization passes
365-
function_pass_manager.doInitialization();
344+
functionPassManager.doInitialization();
366345
for (llvm::Module::iterator i = module->begin(); i != module->end(); i++) {
367-
function_pass_manager.run(*i);
368-
}
369-
function_pass_manager.doFinalization();
370-
module_pass_manager.run(*module);
346+
functionPassManager.run(*i);
347+
348+
functionPassManager.doFinalization();
349+
modulePassManager.run(*module);
371350

372-
Halide::Internal::debug(3) << "After LLVM optimizations:\n";
373-
if (Halide::Internal::debug::debug_level() >= 2) {
374-
#if LLVM_VERSION_MAJOR >= 5
351+
if (FLAGS_llvm_dump_after_opt) {
375352
module->print(llvm::dbgs(), nullptr, false, true);
376-
#else
377-
module->dump();
378-
#endif
353+
}
379354
}
380-
}
355+
}
381356
};
382357

383358
class LLVMCodegen {
@@ -535,16 +510,17 @@ class LLVMCodegen {
535510
llvm::BasicBlock::Create(llvmCtx, "loop_latch", function);
536511
auto* loopExitBB = llvm::BasicBlock::Create(llvmCtx, "loop_exit", function);
537512

538-
//TODO: integrate query ISL as to whether the relevant loop ought be parallelized
513+
// TODO: integrate query ISL as to whether the relevant loop ought be
514+
// parallelized
539515
bool parallel = false;
540516

541517
llvm::Value* SyncRegion = nullptr;
542518
if (parallel) {
543519
SyncRegion = halide_cg.get_builder().CreateCall(
544-
llvm::Intrinsic::getDeclaration(function->getParent(), llvm::Intrinsic::syncregion_start),
545-
{},
546-
"syncreg"
547-
);
520+
llvm::Intrinsic::getDeclaration(
521+
function->getParent(), llvm::Intrinsic::syncregion_start),
522+
{},
523+
"syncreg");
548524
}
549525

550526
halide_cg.get_builder().CreateBr(headerBB);
@@ -596,8 +572,10 @@ class LLVMCodegen {
596572
halide_cg.get_builder().SetInsertPoint(loopBodyBB);
597573

598574
if (parallel) {
599-
auto* detachedBB = llvm::BasicBlock::Create(llvmCtx, "det.achd", function);
600-
halide_cg.get_builder().CreateDetach(detachedBB, loopLatchBB, SyncRegion);
575+
auto* detachedBB =
576+
llvm::BasicBlock::Create(llvmCtx, "det.achd", function);
577+
halide_cg.get_builder().CreateDetach(
578+
detachedBB, loopLatchBB, SyncRegion);
601579
halide_cg.get_builder().SetInsertPoint(detachedBB);
602580
}
603581
auto* currentBB = emitAst(node.for_get_body());
@@ -624,9 +602,9 @@ class LLVMCodegen {
624602
halide_cg.get_builder().SetInsertPoint(loopExitBB);
625603
halide_cg.sym_pop(node.for_get_iterator().get_id().get_name());
626604
if (parallel) {
627-
auto* syncBB = llvm::BasicBlock::Create(llvmCtx, "synced", function);
628-
halide_cg.get_builder().CreateSync(syncBB, SyncRegion);
629-
halide_cg.get_builder().SetInsertPoint(syncBB);
605+
auto* syncBB = llvm::BasicBlock::Create(llvmCtx, "synced", function);
606+
halide_cg.get_builder().CreateSync(syncBB, SyncRegion);
607+
halide_cg.get_builder().SetInsertPoint(syncBB);
630608
}
631609
return halide_cg.get_builder().GetInsertBlock();
632610
}
@@ -695,7 +673,8 @@ class LLVMCodegen {
695673
CodeGen_TC halide_cg;
696674
};
697675

698-
// Create a list of isl ids to be used as loop iterators when building the AST.
676+
// Create a list of isl ids to be used as loop iterators when building the
677+
// AST.
699678
//
700679
// Note that this function can be scrapped as ISL can generate some default
701680
// iterator names. However, it may come handy for associating extra info with

src/core/polyhedral/llvm_jit.cc

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include <stdexcept>
17+
1618
#include "tc/core/polyhedral/llvm_jit.h"
1719

1820
#include "llvm/ExecutionEngine/ExecutionEngine.h"
@@ -29,16 +31,55 @@
2931

3032
using namespace llvm;
3133

34+
// Parse through ldconfig to find the path of a particular
35+
// shared library. This is an unfortunate way to have to
36+
// find it, but I couldn't immediately find something in
37+
// imported libraries that would resolve this for us.
38+
std::string find_library_path(std::string library) {
39+
std::string command = "ldconfig -p | grep " + library;
40+
41+
FILE* fpipe = popen(command.c_str(), "r");
42+
43+
if (fpipe == nullptr) {
44+
throw std::runtime_error("Failed to popen()");
45+
}
46+
47+
std::string output;
48+
char buffer[512];
49+
50+
while (1) {
51+
int charactersRead = fread(buffer, 1, sizeof(buffer), fpipe);
52+
if (charactersRead == 0)
53+
break;
54+
output += std::string(buffer, charactersRead);
55+
}
56+
pclose(fpipe);
57+
58+
int idx = output.rfind("=> ");
59+
if (idx == std::string::npos) {
60+
throw std::runtime_error("Failed locate library: " + library);
61+
}
62+
output = output.substr(idx + 3);
63+
if (output.length() > 0 && output[output.length() - 1] == '\n') {
64+
output = output.substr(0, output.length() - 1);
65+
}
66+
return output;
67+
}
68+
3269
namespace tc {
3370

3471
Jit::Jit()
3572
: TM_(EngineBuilder().selectTarget()),
3673
DL_(TM_->createDataLayout()),
3774
objectLayer_([]() { return std::make_shared<SectionMemoryManager>(); }),
3875
compileLayer_(objectLayer_, orc::SimpleCompiler(*TM_)) {
39-
sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
4076
std::string err;
41-
sys::DynamicLibrary::LoadLibraryPermanently("cilkrts", &err);
77+
78+
auto path = find_library_path("libcilkrts.so");
79+
sys::DynamicLibrary::LoadLibraryPermanently(path.c_str(), &err);
80+
if (err != "") {
81+
throw std::runtime_error("Failed to find cilkrts: " + err);
82+
}
4283
}
4384

4485
void Jit::codegenScop(
@@ -84,4 +125,4 @@ JITTargetAddress Jit::getSymbolAddress(const std::string Name) {
84125
return *res;
85126
}
86127

87-
}
128+
} // namespace tc

test/test_mapper_llvm.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ TEST(LLVMCodegen, MultiStmt) {
6868
{
6969
O1(i, j) +=! A(i, j, rk, rl) * B(i, j)
7070
O2(i, j) = C(i, j) * D(i, j)
71-
O3(i, j) = O1(i, j) + O2(i, j)
71+
O3(i, j) = O1(i, j) + O2(i, j)
7272
}
7373
)TC";
7474

third-party/halide

Submodule halide updated 75 files

0 commit comments

Comments
 (0)