CIRCT 23.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
40 /// Metadata for a tracked signal in the trace.
41 struct Signal {
42 std::string name;
43 unsigned width;
44 };
45
46 /// Create an empty trace for the given top-level design/module name.
47 explicit BMCTrace(llvm::StringRef topName = "bmc");
48
49 /// Register a signal to be tracked and return its stable index. Signals may
50 /// have zero width to represent i0 values.
51 size_t addSignal(llvm::StringRef name, unsigned width);
52 /// Record the value handle for a tracked signal at the given cycle.
53 void record(size_t step, size_t signal, Handle handle);
54 /// Register a signal by name if necessary and record its value handle at the
55 /// given cycle.
56 void record(size_t step, llvm::StringRef name, unsigned width, Handle handle);
57
58 llvm::StringRef getTopName() const { return topName; }
59 llvm::ArrayRef<Signal> getSignals() const { return signals; }
60 size_t getNumSteps() const { return recorded.size(); }
61 /// Return the recorded handle for a signal at a given cycle, if present.
62 std::optional<Handle> lookup(size_t step, size_t signal) const;
63
64 /// Render the trace as cycle-by-cycle text using the provided evaluator to
65 /// materialize values from recorded handles.
66 bool printTextTrace(llvm::raw_ostream &os, Evaluator evaluate) const;
67
68private:
69 /// Per-cycle storage for all tracked signals.
70 using Step = std::vector<std::optional<Handle>>;
71
72 void ensureStep(size_t step);
73
74 std::string topName;
75 std::vector<Signal> signals;
76 llvm::StringMap<size_t> signalIndices;
77 std::vector<Step> recorded;
78};
79
80/// Runtime entry point called by JIT-compiled BMC code.
81extern "C" void circt_bmc_record_trace(BMCTrace *trace, uint32_t step,
82 const char *name, uint32_t width,
83 BMCTrace::Handle handle);
84
85} // namespace circt::bmc
86
87#endif // CIRCT_TOOLS_CIRCT_BMC_BMCTRACE_H
llvm::StringRef getTopName() const
Definition BMCTrace.h:58
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:56
size_t getNumSteps() const
Definition BMCTrace.h:60
void ensureStep(size_t step)
Definition BMCTrace.cpp:25
std::vector< Step > recorded
Definition BMCTrace.h:77
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:59
size_t addSignal(llvm::StringRef name, unsigned width)
Register a signal to be tracked and return its stable index.
Definition BMCTrace.cpp:16
std::vector< std::optional< Handle > > Step
Per-cycle storage for all tracked signals.
Definition BMCTrace.h:70
llvm::StringMap< size_t > signalIndices
Definition BMCTrace.h:76
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:74
std::vector< Signal > signals
Definition BMCTrace.h:75
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:30
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:50
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:78
Metadata for a tracked signal in the trace.
Definition BMCTrace.h:41