Skip to content

Commit 0c7d9d5

Browse files
committed
[SandboxIR][Bench] Initial patch for performance tracking
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 c1a8283 commit 0c7d9d5

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

llvm/benchmarks/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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)
11+
add_benchmark(SandboxIRBench SandboxIRBench.cpp PARTIAL_SOURCES_INTENDED)

llvm/benchmarks/SandboxIRBench.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 (std::is_same<typename TypeSelect<IRTy>::BasicBlock,
62+
llvm::BasicBlock>::value)
63+
return LLVMBB;
64+
else
65+
return BB;
66+
}
67+
68+
static std::string generateBBWalkIR(unsigned Size) {
69+
std::stringstream SS;
70+
SS << "define void @foo(i32 %v1, i32 %v2) {\n";
71+
for (auto Cnt : seq<unsigned>(0, Size))
72+
SS << " %add" << Cnt << " = add i32 %v1, %v2\n";
73+
SS << "ret void";
74+
SS << "}";
75+
return SS.str();
76+
}
77+
78+
template <IR IRTy> static void BBWalk(benchmark::State &State) {
79+
LLVMContext LLVMCtx;
80+
sandboxir::Context Ctx(LLVMCtx);
81+
unsigned NumInstrs = State.range(0);
82+
std::unique_ptr<llvm::Module> LLVMM;
83+
auto *BB = genIR<IRTy>(LLVMM, LLVMCtx, Ctx, generateBBWalkIR, NumInstrs);
84+
for (auto _ : State) {
85+
// Walk LLVM Instructions.
86+
for (auto &I : *BB)
87+
benchmark::DoNotOptimize(I);
88+
}
89+
}
90+
91+
static std::string generateGetTypeIR(unsigned Size) {
92+
return R"IR(
93+
define void @foo(i32 %v1, i32 %v2) {
94+
%add = add i32 %v1, %v2
95+
ret void
96+
}
97+
)IR";
98+
}
99+
100+
template <IR IRTy> static void GetType(benchmark::State &State) {
101+
LLVMContext LLVMCtx;
102+
sandboxir::Context Ctx(LLVMCtx);
103+
std::unique_ptr<llvm::Module> LLVMM;
104+
auto *BB = genIR<IRTy>(LLVMM, LLVMCtx, Ctx, generateGetTypeIR);
105+
auto *I = &*BB->begin();
106+
for (auto _ : State)
107+
benchmark::DoNotOptimize(I->getType());
108+
}
109+
110+
BENCHMARK(GetType<IR::LLVM>);
111+
BENCHMARK(GetType<IR::SBox>);
112+
113+
BENCHMARK(BBWalk<IR::LLVM>)->Args({1024});
114+
BENCHMARK(BBWalk<IR::SBox>)->Args({1024});
115+
116+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)