CIRCT 24.0.0git
Loading...
Searching...
No Matches
BMCTrace.h
Go to the documentation of this file.
1//===- BMCTrace.h - Runtime trace storage for circt-bmc ---------*- C++ -*-===//
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// This file defines the runtime trace store used by circt-bmc.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_TOOLS_CIRCT_BMC_BMCTRACE_H
14#define CIRCT_TOOLS_CIRCT_BMC_BMCTRACE_H
15
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/FunctionExtras.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/raw_ostream.h"
22
23#include <cstdint>
24#include <optional>
25#include <string>
26#include <vector>
27
28namespace circt::bmc {
29
30class BMCTrace {
31public:
32 /// Opaque per-signal value reference recorded for a specific cycle.
33 using Handle = const void *;
34 /// Callback used to materialize a recorded handle into a concrete value when
35 /// formatting a trace. The provided width is the declared width of the
36 /// signal, which may be zero for i0 values.
37 using Evaluator =
38 llvm::function_ref<std::optional<llvm::APInt>(Handle, unsigned width)>;
39 /// Z3 API callbacks passed in by JIT-compiled code. Keeping these as opaque
40 /// function pointers avoids making the runtime library depend on Z3 headers
41 /// or a particular Z3 shared library at link time.
42 using ModelEval = bool (*)(Handle context, Handle model, Handle expression,
43 bool modelCompletion, Handle *value);
44 using GetNumeralBinaryString = const char *(*)(Handle context, Handle value);
45
46 /// Metadata for a tracked signal in the trace.
47 struct Signal {
48 std::string name;
49 unsigned width;
50 };
51
52 /// Create an empty trace for the given top-level design/module name.
53 explicit BMCTrace(llvm::StringRef topName = "bmc");
54
55 /// Register a signal to be tracked and return its stable index. Signals may
56 /// have zero width to represent i0 values.
57 size_t addSignal(llvm::StringRef name, unsigned width);
58 /// Record the value handle for a tracked signal at the given cycle.
59 void record(size_t step, size_t signal, Handle handle);
60 /// Register a signal by name if necessary and record its value handle at the
61 /// given cycle.
62 void record(size_t step, llvm::StringRef name, unsigned width, Handle handle);
63
64 llvm::StringRef getTopName() const { return topName; }
65 llvm::ArrayRef<Signal> getSignals() const { return signals; }
66 size_t getNumSteps() const { return recorded.size(); }
67 /// Return the recorded handle for a signal at a given cycle, if present.
68 std::optional<Handle> lookup(size_t step, size_t signal) const;
69
70 /// Render the trace as cycle-by-cycle text using the provided evaluator to
71 /// materialize values from recorded handles.
72 bool printTextTrace(llvm::raw_ostream &os, Evaluator evaluate) const;
73 /// Render the trace by evaluating its recorded Z3 ASTs in a satisfying
74 /// model. This must run before the owning Z3 context is destroyed.
75 bool printTextTrace(llvm::raw_ostream &os, Handle context, Handle model,
76 ModelEval modelEval,
77 GetNumeralBinaryString getNumeralBinaryString) const;
78
79private:
80 /// Per-cycle storage for all tracked signals.
81 using Step = std::vector<std::optional<Handle>>;
82
83 void ensureStep(size_t step);
84
85 std::string topName;
86 std::vector<Signal> signals;
87 llvm::StringMap<size_t> signalIndices;
88 std::vector<Step> recorded;
89};
90
91/// Runtime entry point called by JIT-compiled BMC code.
92extern "C" void circt_bmc_record_trace(BMCTrace *trace, uint32_t step,
93 const char *name, uint32_t width,
94 BMCTrace::Handle handle);
95
96/// Runtime entry point called on the SAT path while the Z3 context and model
97/// are still alive.
98extern "C" bool
100 BMCTrace::Handle model, BMCTrace::ModelEval modelEval,
101 BMCTrace::GetNumeralBinaryString getNumeralBinaryString);
102
103} // namespace circt::bmc
104
105#endif // CIRCT_TOOLS_CIRCT_BMC_BMCTRACE_H
static std::unique_ptr< Context > context
llvm::StringRef getTopName() const
Definition BMCTrace.h:64
bool printTextTrace(llvm::raw_ostream &os, Evaluator evaluate) const
Render the trace as cycle-by-cycle text using the provided evaluator to materialize values from recor...
Definition BMCTrace.cpp:57
const char *(*)(Handle context, Handle value) GetNumeralBinaryString
Definition BMCTrace.h:44
size_t getNumSteps() const
Definition BMCTrace.h:66
void ensureStep(size_t step)
Definition BMCTrace.cpp:26
std::vector< Step > recorded
Definition BMCTrace.h:88
const void * Handle
Opaque per-signal value reference recorded for a specific cycle.
Definition BMCTrace.h:33
llvm::ArrayRef< Signal > getSignals() const
Definition BMCTrace.h:65
size_t addSignal(llvm::StringRef name, unsigned width)
Register a signal to be tracked and return its stable index.
Definition BMCTrace.cpp:17
std::vector< std::optional< Handle > > Step
Per-cycle storage for all tracked signals.
Definition BMCTrace.h:81
llvm::StringMap< size_t > signalIndices
Definition BMCTrace.h:87
llvm::function_ref< std::optional< llvm::APInt >(Handle, unsigned width)> Evaluator
Callback used to materialize a recorded handle into a concrete value when formatting a trace.
Definition BMCTrace.h:38
std::string topName
Definition BMCTrace.h:85
std::vector< Signal > signals
Definition BMCTrace.h:86
void record(size_t step, size_t signal, Handle handle)
Record the value handle for a tracked signal at the given cycle.
Definition BMCTrace.cpp:31
std::optional< Handle > lookup(size_t step, size_t signal) const
Return the recorded handle for a signal at a given cycle, if present.
Definition BMCTrace.cpp:51
bool(*)(Handle context, Handle model, Handle expression, bool modelCompletion, Handle *value) ModelEval
Z3 API callbacks passed in by JIT-compiled code.
Definition BMCTrace.h:43
bool circt_bmc_print_trace(BMCTrace *trace, BMCTrace::Handle context, BMCTrace::Handle model, BMCTrace::ModelEval modelEval, BMCTrace::GetNumeralBinaryString getNumeralBinaryString)
Runtime entry point called on the SAT path while the Z3 context and model are still alive.
Definition BMCTrace.cpp:122
void circt_bmc_record_trace(BMCTrace *trace, uint32_t step, const char *name, uint32_t width, BMCTrace::Handle handle)
Runtime entry point called by JIT-compiled BMC code.
Definition BMCTrace.cpp:112
Metadata for a tracked signal in the trace.
Definition BMCTrace.h:47