Skip to content

Commit 15c5a81

Browse files
committed
Add circuit location statistics for recursion predicates
* Overhaul EDSL location handling; we now generate mlir Locations directly instead of keeping our own SourceLoc structure, and it's relatively easy to add additional context to component construction using ScopedSourceLoc. * Expand --op-stats flag to work on `gen_predicates`; this outputs `...-opstats.txt` files in the output directory for each predicate processed. * Add --op-stats-flat-cost flag to count each instruction the same. (This is still not the same as the number of cycles in the ZKR, since the statistics are collected before encoding) * Added a bunch more source annotations to get better granularity for recursion predicates.
1 parent 8051576 commit 15c5a81

File tree

27 files changed

+398
-464
lines changed

27 files changed

+398
-464
lines changed

risc0/core/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ cc_library(
88
hdrs = [
99
"elf.h",
1010
"log.h",
11-
"source_loc.h",
1211
"util.h",
1312
],
1413
visibility = ["//visibility:public"],

risc0/core/source_loc.h

Lines changed: 0 additions & 78 deletions
This file was deleted.

zirgen/circuit/predicates/gen_predicates.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "zirgen/circuit/verify/wrap_rv32im.h"
2020
#include "zirgen/circuit/verify/wrap_zirgen.h"
2121
#include "zirgen/compiler/codegen/codegen.h"
22+
#include "zirgen/compiler/stats/OpStats.h"
2223

2324
using namespace zirgen;
2425
using namespace zirgen::verify;
@@ -202,6 +203,7 @@ static cl::opt<std::string>
202203
int main(int argc, char* argv[]) {
203204
llvm::InitLLVM y(argc, argv);
204205
registerEdslCLOptions();
206+
registerOpStatsCLOptions();
205207
llvm::cl::ParseCommandLineOptions(argc, argv, "gen_predicates edsl");
206208

207209
Module module;

zirgen/circuit/recursion/recursion.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
using namespace zirgen;
2222
using namespace zirgen::recursion;
23-
using namespace risc0;
2423
using namespace mlir;
2524

2625
int main(int argc, char* argv[]) {

zirgen/circuit/rv32im/v1/edsl/rv32im.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
using namespace zirgen;
2525
using namespace zirgen::rv32im_v1;
26-
using namespace risc0;
2726
using namespace mlir;
2827

2928
int main(int argc, char* argv[]) {

zirgen/circuit/verify/fri.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Val fold_eval(const std::vector<Val>& values, Val x) {
7373
}
7474

7575
Val dynamic_pow(Val in, Val pow, size_t maxPow) {
76+
ScopedLocation loc;
77+
7678
Val out = 1;
7779
Val mul = in;
7880
for (size_t i = 0; i < log2Ceil(maxPow); i++) {
@@ -98,6 +100,8 @@ struct VerifyRoundInfo {
98100
, mix(iop.rngExtVal()) {}
99101

100102
void verifyQuery(ReadIopVal& iop, Val* pos, Val* goal) const {
103+
ScopedLocation loc;
104+
101105
// Compute which group we are in
102106
Val group = *pos & (domain - 1);
103107
Val qout = (*pos - group) / domain;
@@ -117,6 +121,8 @@ struct VerifyRoundInfo {
117121

118122
// Verify a FRI proof,
119123
void friVerify(ReadIopVal& iop, size_t deg, InnerVerify inner) {
124+
ScopedLocation loc;
125+
120126
size_t domain = deg * kInvRate;
121127
size_t origDomain = domain;
122128
std::vector<VerifyRoundInfo> rounds;

zirgen/circuit/verify/merkle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ DigestVal MerkleTreeVerifier::getRoot() const {
6464
}
6565

6666
std::vector<Val> MerkleTreeVerifier::verify(ReadIopVal& iop, Val idx) const {
67+
ScopedLocation loc;
68+
6769
std::vector<Val> out;
6870
if (useExtension) {
6971
out = iop.readExtVals(colSize);

zirgen/circuit/verify/poly.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
namespace zirgen::verify {
1818

1919
Val poly_eval(const std::vector<Val>& coeffs, Val x) {
20+
ScopedLocation loc;
21+
2022
Val tot = 0;
2123
Val mul = 1;
2224
for (size_t i = 0; i < coeffs.size(); i++) {

zirgen/circuit/verify/verify.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ template <typename T> T dbg(std::string fmt, T arg) {
3838
} // namespace
3939

4040
VerifyInfo verify(ReadIopVal& iop, size_t po2, const CircuitInterface& circuit) {
41+
ScopedLocation loc;
42+
4143
VerifyInfo verifyInfo;
4244

4345
// At the start of verification, add the version strings to the Fiat-Shamir transcript.
@@ -177,11 +179,22 @@ VerifyInfo verify(ReadIopVal& iop, size_t po2, const CircuitInterface& circuit)
177179
}
178180
// Finally, do a FRI verification
179181
friVerify(iop, size, [&](ReadIopVal& iop, Val idx) {
182+
ScopedLocation loc;
183+
180184
auto x = dynamic_pow(kRouFwd[log2Ceil(domain)], idx, domain);
181185
std::map<unsigned, std::vector<Val>> rows;
182-
rows[/*REGISTER_GROUP_ACCUM*/ 0] = accumMerkle.verify(iop, idx);
183-
rows[/*REGISTER_GROUP_CODE=*/1] = codeMerkle.verify(iop, idx);
184-
rows[/*REGISTER_GROUP_DATA=*/2] = dataMerkle.verify(iop, idx);
186+
{
187+
ScopedLocation loc;
188+
rows[/*REGISTER_GROUP_ACCUM*/ 0] = accumMerkle.verify(iop, idx);
189+
}
190+
{
191+
ScopedLocation loc;
192+
rows[/*REGISTER_GROUP_CODE=*/1] = codeMerkle.verify(iop, idx);
193+
}
194+
{
195+
ScopedLocation loc;
196+
rows[/*REGISTER_GROUP_DATA=*/2] = dataMerkle.verify(iop, idx);
197+
}
185198
auto checkRow = checkMerkle.verify(iop, idx);
186199
Val curMix = 1;
187200
std::vector<Val> tot(comboU.size(), 0);
@@ -219,6 +232,8 @@ VerifyInfo verifyRecursion(ReadIopVal& allowedRoot,
219232
std::vector<ReadIopVal> seals,
220233
std::vector<ReadIopVal> alloweds,
221234
const CircuitInterface& circuit) {
235+
ScopedLocation loc;
236+
222237
VerifyInfo verifyInfo;
223238
verifyInfo.codeRoot = allowedRoot.readDigests(1)[0];
224239

zirgen/compiler/codegen/gen_cpp.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ struct CppStreamEmitterImpl : CppStreamEmitter {
3737
void header(func::FuncOp func) {
3838
ofs << "// This code is automatically generated\n\n";
3939
ofs << "#include \"impl.h\"\n\n";
40-
ofs << "using namespace risc0;\n\n";
4140
ofs << "namespace circuit::" << func.getName() << " {\n\n";
4241
}
4342

0 commit comments

Comments
 (0)