-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SandboxIR][Bench] Initial patch for performance tracking #107296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
AsmParser | ||
Core | ||
SandboxIR | ||
Support) | ||
|
||
add_benchmark(DummyYAML DummyYAML.cpp PARTIAL_SOURCES_INTENDED) | ||
add_benchmark(xxhash xxhash.cpp PARTIAL_SOURCES_INTENDED) | ||
add_benchmark(GetIntrinsicForClangBuiltin GetIntrinsicForClangBuiltin.cpp PARTIAL_SOURCES_INTENDED) | ||
add_benchmark(FormatVariadicBM FormatVariadicBM.cpp PARTIAL_SOURCES_INTENDED) | ||
add_benchmark(GetIntrinsicInfoTableEntriesBM GetIntrinsicInfoTableEntriesBM.cpp PARTIAL_SOURCES_INTENDED) | ||
add_benchmark(SandboxIRBench SandboxIRBench.cpp PARTIAL_SOURCES_INTENDED) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
//===- SandboxIRBench.cpp -------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// These tests measure the performance of some core SandboxIR functions and | ||
// compare them against LLVM IR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "benchmark/benchmark.h" | ||
#include "llvm/AsmParser/Parser.h" | ||
#include "llvm/IR/BasicBlock.h" | ||
#include "llvm/IR/DataLayout.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/Instruction.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/SandboxIR/SandboxIR.h" | ||
#include "llvm/Support/SourceMgr.h" | ||
#include <memory> | ||
|
||
using namespace llvm; | ||
|
||
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { | ||
SMDiagnostic Err; | ||
std::unique_ptr<Module> M = parseAssemblyString(IR, Err, C); | ||
if (!M) | ||
Err.print("SandboxIRBench", errs()); | ||
return M; | ||
} | ||
|
||
enum class IR { | ||
LLVM, | ||
SBox, | ||
}; | ||
// Traits to get llvm::BasicBlock/sandboxir::BasicBlock from IR::LLVM/IR::SBox. | ||
template <IR IRTy> struct TypeSelect {}; | ||
template <> struct TypeSelect<IR::LLVM> { | ||
using BasicBlock = llvm::BasicBlock; | ||
}; | ||
template <> struct TypeSelect<IR::SBox> { | ||
using BasicBlock = sandboxir::BasicBlock; | ||
}; | ||
|
||
template <IR IRTy> | ||
static typename TypeSelect<IRTy>::BasicBlock * | ||
genIR(std::unique_ptr<llvm::Module> &LLVMM, LLVMContext &LLVMCtx, | ||
sandboxir::Context &Ctx, | ||
std::function<std::string(unsigned)> GenerateIRStr, | ||
unsigned NumInstrs = 0u) { | ||
std::string IRStr = GenerateIRStr(NumInstrs); | ||
LLVMM = parseIR(LLVMCtx, IRStr.c_str()); | ||
llvm::Function *LLVMF = &*LLVMM->getFunction("foo"); | ||
llvm::BasicBlock *LLVMBB = &*LLVMF->begin(); | ||
|
||
sandboxir::Function *F = Ctx.createFunction(LLVMF); | ||
sandboxir::BasicBlock *BB = &*F->begin(); | ||
if constexpr (IRTy == IR::LLVM) | ||
return LLVMBB; | ||
else | ||
return BB; | ||
} | ||
|
||
static std::string generateBBWalkIR(unsigned Size) { | ||
std::stringstream SS; | ||
SS << "define void @foo(i32 %v1, i32 %v2) {\n"; | ||
for (auto Cnt : seq<unsigned>(0, Size)) | ||
SS << " %add" << Cnt << " = add i32 %v1, %v2\n"; | ||
SS << "ret void"; | ||
SS << "}"; | ||
return SS.str(); | ||
} | ||
|
||
template <IR IRTy> static void BBWalk(benchmark::State &State) { | ||
LLVMContext LLVMCtx; | ||
sandboxir::Context Ctx(LLVMCtx); | ||
unsigned NumInstrs = State.range(0); | ||
std::unique_ptr<llvm::Module> LLVMM; | ||
auto *BB = genIR<IRTy>(LLVMM, LLVMCtx, Ctx, generateBBWalkIR, NumInstrs); | ||
for (auto _ : State) { | ||
// Walk LLVM Instructions. | ||
for (auto &I : *BB) | ||
benchmark::DoNotOptimize(I); | ||
} | ||
} | ||
|
||
static std::string generateGetTypeIR(unsigned Size) { | ||
return R"IR( | ||
define void @foo(i32 %v1, i32 %v2) { | ||
%add = add i32 %v1, %v2 | ||
ret void | ||
} | ||
)IR"; | ||
} | ||
|
||
template <IR IRTy> static void GetType(benchmark::State &State) { | ||
LLVMContext LLVMCtx; | ||
sandboxir::Context Ctx(LLVMCtx); | ||
std::unique_ptr<llvm::Module> LLVMM; | ||
auto *BB = genIR<IRTy>(LLVMM, LLVMCtx, Ctx, generateGetTypeIR); | ||
auto *I = &*BB->begin(); | ||
for (auto _ : State) | ||
benchmark::DoNotOptimize(I->getType()); | ||
} | ||
|
||
BENCHMARK(GetType<IR::LLVM>); | ||
BENCHMARK(GetType<IR::SBox>); | ||
|
||
BENCHMARK(BBWalk<IR::LLVM>)->Args({1024}); | ||
BENCHMARK(BBWalk<IR::SBox>)->Args({1024}); | ||
|
||
BENCHMARK_MAIN(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patch is adding benchmarking for LLVM-IR vs Sandbox-IR traversal, but it will not benchmark the creation of SandboxIR, as it's done in both currently. Can you add this in a follow up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will add this in a followup patch.