Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | #ifndef OPENVDB_AX_UNITTEST_BACKEND_UTIL_HAS_BEEN_INCLUDED | ||
5 | #define OPENVDB_AX_UNITTEST_BACKEND_UTIL_HAS_BEEN_INCLUDED | ||
6 | |||
7 | #include <openvdb_ax/codegen/Types.h> | ||
8 | |||
9 | #include <llvm/IR/LLVMContext.h> | ||
10 | #include <llvm/IR/Module.h> | ||
11 | #include <llvm/IR/IRBuilder.h> | ||
12 | #include <llvm/ExecutionEngine/ExecutionEngine.h> | ||
13 | #include <llvm/Support/Host.h> | ||
14 | |||
15 | #include <memory> | ||
16 | #include <string> | ||
17 | |||
18 | namespace unittest_util | ||
19 | { | ||
20 | |||
21 |
1/2✓ Branch 2 taken 281 times.
✗ Branch 3 not taken.
|
310 | struct LLVMState |
22 | { | ||
23 | 310 | LLVMState(const std::string& name = "__test_module") | |
24 |
3/6✓ Branch 2 taken 310 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 310 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 310 times.
✗ Branch 9 not taken.
|
310 | : mCtx(new llvm::LLVMContext), mModule(new llvm::Module(name, *mCtx)) {} |
25 | |||
26 | llvm::LLVMContext& context() { return *mCtx; } | ||
27 | llvm::Module& module() { return *mModule; } | ||
28 | |||
29 | inline llvm::BasicBlock* | ||
30 | 9 | scratchBlock(const std::string& functionName = "TestFunction", | |
31 | const std::string& blockName = "TestEntry") | ||
32 | { | ||
33 | llvm::FunctionType* type = | ||
34 | 9 | llvm::FunctionType::get(openvdb::ax::codegen::LLVMType<void>::get(this->context()), | |
35 | /**var-args*/false); | ||
36 | llvm::Function* dummyFunction = | ||
37 | 9 | llvm::Function::Create(type, llvm::Function::ExternalLinkage, functionName, &this->module()); | |
38 | 9 | return llvm::BasicBlock::Create(this->context(), blockName, dummyFunction); | |
39 | } | ||
40 | |||
41 | 9 | inline std::unique_ptr<llvm::ExecutionEngine> EE() | |
42 | { | ||
43 |
2/4✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
|
9 | mModule->setTargetTriple(llvm::sys::getDefaultTargetTriple()); |
44 | 9 | llvm::StringMap<bool> HostFeatures; | |
45 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | llvm::sys::getHostCPUFeatures(HostFeatures); |
46 | std::vector<llvm::StringRef> features; | ||
47 |
2/2✓ Branch 0 taken 675 times.
✓ Branch 1 taken 9 times.
|
684 | for (auto& feature : HostFeatures) { |
48 |
3/6✓ Branch 0 taken 369 times.
✓ Branch 1 taken 306 times.
✓ Branch 3 taken 369 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
675 | if (feature.second) features.emplace_back(feature.first()); |
49 | } | ||
50 | |||
51 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | auto M = std::move(mModule); |
52 |
3/6✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
|
9 | mModule.reset(new llvm::Module(M->getName(), *mCtx)); |
53 | std::unique_ptr<llvm::ExecutionEngine> | ||
54 |
2/4✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
|
18 | EE(llvm::EngineBuilder(std::move(M)) |
55 | .setEngineKind(llvm::EngineKind::JIT) | ||
56 | .setOptLevel(llvm::CodeGenOpt::Level::Default) | ||
57 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | .setMCPU(llvm::sys::getHostCPUName()) |
58 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | .setMAttrs(features) |
59 | .create()); | ||
60 | |||
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | assert(EE.get()); |
62 | 9 | return EE; | |
63 | } | ||
64 | |||
65 | private: | ||
66 | std::unique_ptr<llvm::LLVMContext> mCtx; | ||
67 | std::unique_ptr<llvm::Module> mModule; | ||
68 | }; | ||
69 | |||
70 | } | ||
71 | |||
72 | #endif // OPENVDB_AX_UNITTEST_BACKEND_UTIL_HAS_BEEN_INCLUDED | ||
73 | |||
74 |