Skip to content

Commit 5e25291

Browse files
authored
[SandboxIR][Bench] Initial patch for performance tracking (#107296)
This patch adds a new benchmark suite for SandboxIR. It measures the performance of some of the most commonly used API functions and compares it against LLVM IR.
1 parent 311ac63 commit 5e25291

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

llvm/benchmarks/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
set(LLVM_LINK_COMPONENTS
2+
AsmParser
23
Core
4+
SandboxIR
35
Support)
46

57
add_benchmark(DummyYAML DummyYAML.cpp PARTIAL_SOURCES_INTENDED)
68
add_benchmark(xxhash xxhash.cpp PARTIAL_SOURCES_INTENDED)
79
add_benchmark(GetIntrinsicForClangBuiltin GetIntrinsicForClangBuiltin.cpp PARTIAL_SOURCES_INTENDED)
810
add_benchmark(FormatVariadicBM FormatVariadicBM.cpp PARTIAL_SOURCES_INTENDED)
911
add_benchmark(GetIntrinsicInfoTableEntriesBM GetIntrinsicInfoTableEntriesBM.cpp PARTIAL_SOURCES_INTENDED)
12+
add_benchmark(SandboxIRBench SandboxIRBench.cpp PARTIAL_SOURCES_INTENDED)
13+

llvm/benchmarks/SandboxIRBench.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//===- SandboxIRBench.cpp -------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// These tests measure the performance of some core SandboxIR functions and
10+
// compare them against LLVM IR.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "benchmark/benchmark.h"
15+
#include "llvm/AsmParser/Parser.h"
16+
#include "llvm/IR/BasicBlock.h"
17+
#include "llvm/IR/DataLayout.h"
18+
#include "llvm/IR/Function.h"
19+
#include "llvm/IR/Instruction.h"
20+
#include "llvm/IR/Module.h"
21+
#include "llvm/SandboxIR/SandboxIR.h"
22+
#include "llvm/Support/SourceMgr.h"
23+
#include <memory>
24+
25+
using namespace llvm;
26+
27+
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
28+
SMDiagnostic Err;
29+
std::unique_ptr<Module> M = parseAssemblyString(IR, Err, C);
30+
if (!M)
31+
Err.print("SandboxIRBench", errs());
32+
return M;
33+
}
34+
35+
enum class IR {
36+
LLVM,
37+
SBox,
38+
};
39+
// Traits to get llvm::BasicBlock/sandboxir::BasicBlock from IR::LLVM/IR::SBox.
40+
template <IR IRTy> struct TypeSelect {};
41+
template <> struct TypeSelect<IR::LLVM> {
42+
using BasicBlock = llvm::BasicBlock;
43+
};
44+
template <> struct TypeSelect<IR::SBox> {
45+
using BasicBlock = sandboxir::BasicBlock;
46+
};
47+
48+
template <IR IRTy>
49+
static typename TypeSelect<IRTy>::BasicBlock *
50+
genIR(std::unique_ptr<llvm::Module> &LLVMM, LLVMContext &LLVMCtx,
51+
sandboxir::Context &Ctx,
52+
std::function<std::string(unsigned)> GenerateIRStr,
53+
unsigned NumInstrs = 0u) {
54+
std::string IRStr = GenerateIRStr(NumInstrs);
55+
LLVMM = parseIR(LLVMCtx, IRStr.c_str());
56+
llvm::Function *LLVMF = &*LLVMM->getFunction("foo");
57+
llvm::BasicBlock *LLVMBB = &*LLVMF->begin();
58+
59+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
60+
sandboxir::BasicBlock *BB = &*F->begin();
61+
if constexpr (IRTy == IR::LLVM)
62+
return LLVMBB;
63+
else
64+
return BB;
65+
}
66+
67+
static std::string generateBBWalkIR(unsigned Size) {
68+
std::stringstream SS;
69+
SS << "define void @foo(i32 %v1, i32 %v2) {\n";
70+
for (auto Cnt : seq<unsigned>(0, Size))
71+
SS << " %add" << Cnt << " = add i32 %v1, %v2\n";
72+
SS << "ret void";
73+
SS << "}";
74+
return SS.str();
75+
}
76+
77+
template <IR IRTy> static void BBWalk(benchmark::State &State) {
78+
LLVMContext LLVMCtx;
79+
sandboxir::Context Ctx(LLVMCtx);
80+
unsigned NumInstrs = State.range(0);
81+
std::unique_ptr<llvm::Module> LLVMM;
82+
auto *BB = genIR<IRTy>(LLVMM, LLVMCtx, Ctx, generateBBWalkIR, NumInstrs);
83+
for (auto _ : State) {
84+
// Walk LLVM Instructions.
85+
for (auto &I : *BB)
86+
benchmark::DoNotOptimize(I);
87+
}
88+
}
89+
90+
static std::string generateGetTypeIR(unsigned Size) {
91+
return R"IR(
92+
define void @foo(i32 %v1, i32 %v2) {
93+
%add = add i32 %v1, %v2
94+
ret void
95+
}
96+
)IR";
97+
}
98+
99+
template <IR IRTy> static void GetType(benchmark::State &State) {
100+
LLVMContext LLVMCtx;
101+
sandboxir::Context Ctx(LLVMCtx);
102+
std::unique_ptr<llvm::Module> LLVMM;
103+
auto *BB = genIR<IRTy>(LLVMM, LLVMCtx, Ctx, generateGetTypeIR);
104+
auto *I = &*BB->begin();
105+
for (auto _ : State)
106+
benchmark::DoNotOptimize(I->getType());
107+
}
108+
109+
BENCHMARK(GetType<IR::LLVM>);
110+
BENCHMARK(GetType<IR::SBox>);
111+
112+
BENCHMARK(BBWalk<IR::LLVM>)->Args({1024});
113+
BENCHMARK(BBWalk<IR::SBox>)->Args({1024});
114+
115+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)