[tint][ir] Refactor the Disassembler, rename to Disassembly
Make the bulk of the disassembler private, and invoke the disassemble
function from the constructor. As the class only disassebles once, and
the API is (mostly) immutable, rename the class to Disassembly.
This CL contains a fix for block-parameter values being printed with
their type. No other value did this.
Change-Id: I8df891a06bc0a70d75538340c89d9d1e57757121
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/186461
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/cmd/tint/main.cc b/src/tint/cmd/tint/main.cc
index 68a9d81..f413485 100644
--- a/src/tint/cmd/tint/main.cc
+++ b/src/tint/cmd/tint/main.cc
@@ -43,7 +43,7 @@
#include "src/tint/api/tint.h"
#include "src/tint/cmd/common/generate_external_texture_bindings.h"
#include "src/tint/cmd/common/helper.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/module.h"
#include "src/tint/lang/wgsl/ast/module.h"
#include "src/tint/lang/wgsl/ast/transform/first_index_offset.h"
@@ -1223,7 +1223,7 @@
std::cerr << "Failed to build IR from program: " << result.Failure() << "\n";
return false;
}
- options.printer->Print(tint::core::ir::Disassemble(result.Get()));
+ options.printer->Print(tint::core::ir::Disassemble(result.Get()).Text());
options.printer->Print(tint::StyledText{} << "\n");
return true;
#endif
diff --git a/src/tint/lang/core/ir/BUILD.bazel b/src/tint/lang/core/ir/BUILD.bazel
index a02930e..6617663 100644
--- a/src/tint/lang/core/ir/BUILD.bazel
+++ b/src/tint/lang/core/ir/BUILD.bazel
@@ -57,7 +57,7 @@
"core_binary.cc",
"core_builtin_call.cc",
"core_unary.cc",
- "disassembler.cc",
+ "disassembly.cc",
"discard.cc",
"exit.cc",
"exit_if.cc",
@@ -109,7 +109,7 @@
"core_binary.h",
"core_builtin_call.h",
"core_unary.h",
- "disassembler.h",
+ "disassembly.h",
"discard.h",
"exit.h",
"exit_if.h",
diff --git a/src/tint/lang/core/ir/BUILD.cmake b/src/tint/lang/core/ir/BUILD.cmake
index 94365b4..70bc0d4 100644
--- a/src/tint/lang/core/ir/BUILD.cmake
+++ b/src/tint/lang/core/ir/BUILD.cmake
@@ -78,8 +78,8 @@
lang/core/ir/core_builtin_call.h
lang/core/ir/core_unary.cc
lang/core/ir/core_unary.h
- lang/core/ir/disassembler.cc
- lang/core/ir/disassembler.h
+ lang/core/ir/disassembly.cc
+ lang/core/ir/disassembly.h
lang/core/ir/discard.cc
lang/core/ir/discard.h
lang/core/ir/exit.cc
diff --git a/src/tint/lang/core/ir/BUILD.gn b/src/tint/lang/core/ir/BUILD.gn
index ac59268..9927e42 100644
--- a/src/tint/lang/core/ir/BUILD.gn
+++ b/src/tint/lang/core/ir/BUILD.gn
@@ -80,8 +80,8 @@
"core_builtin_call.h",
"core_unary.cc",
"core_unary.h",
- "disassembler.cc",
- "disassembler.h",
+ "disassembly.cc",
+ "disassembly.h",
"discard.cc",
"discard.h",
"exit.cc",
diff --git a/src/tint/lang/core/ir/binary/roundtrip_fuzz.cc b/src/tint/lang/core/ir/binary/roundtrip_fuzz.cc
index ccc7a9c..7378a3f 100644
--- a/src/tint/lang/core/ir/binary/roundtrip_fuzz.cc
+++ b/src/tint/lang/core/ir/binary/roundtrip_fuzz.cc
@@ -28,7 +28,7 @@
#include "src/tint/cmd/fuzz/ir/fuzz.h"
#include "src/tint/lang/core/ir/binary/decode.h"
#include "src/tint/lang/core/ir/binary/encode.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
namespace tint::core::ir::binary {
namespace {
diff --git a/src/tint/lang/core/ir/binary/roundtrip_test.cc b/src/tint/lang/core/ir/binary/roundtrip_test.cc
index 9d8b368..52a516b 100644
--- a/src/tint/lang/core/ir/binary/roundtrip_test.cc
+++ b/src/tint/lang/core/ir/binary/roundtrip_test.cc
@@ -29,7 +29,7 @@
#include "src/tint/lang/core/ir/binary/decode.h"
#include "src/tint/lang/core/ir/binary/encode.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/type/depth_multisampled_texture.h"
#include "src/tint/lang/core/type/depth_texture.h"
#include "src/tint/lang/core/type/external_texture.h"
diff --git a/src/tint/lang/core/ir/disassembler.cc b/src/tint/lang/core/ir/disassembly.cc
similarity index 84%
rename from src/tint/lang/core/ir/disassembler.cc
rename to src/tint/lang/core/ir/disassembly.cc
index 1664dce..ddc7dba 100644
--- a/src/tint/lang/core/ir/disassembler.cc
+++ b/src/tint/lang/core/ir/disassembly.cc
@@ -25,32 +25,26 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
+#include <memory>
+#include <string_view>
#include "src//tint/lang/core/ir/unary.h"
#include "src/tint/lang/core/constant/composite.h"
#include "src/tint/lang/core/constant/scalar.h"
#include "src/tint/lang/core/constant/splat.h"
-#include "src/tint/lang/core/fluent_types.h"
-#include "src/tint/lang/core/ir/access.h"
#include "src/tint/lang/core/ir/binary.h"
-#include "src/tint/lang/core/ir/bitcast.h"
#include "src/tint/lang/core/ir/block.h"
#include "src/tint/lang/core/ir/block_param.h"
#include "src/tint/lang/core/ir/break_if.h"
-#include "src/tint/lang/core/ir/construct.h"
#include "src/tint/lang/core/ir/continue.h"
-#include "src/tint/lang/core/ir/convert.h"
-#include "src/tint/lang/core/ir/core_builtin_call.h"
#include "src/tint/lang/core/ir/discard.h"
#include "src/tint/lang/core/ir/exit_if.h"
#include "src/tint/lang/core/ir/exit_loop.h"
#include "src/tint/lang/core/ir/exit_switch.h"
+#include "src/tint/lang/core/ir/function.h"
#include "src/tint/lang/core/ir/if.h"
#include "src/tint/lang/core/ir/instruction_result.h"
-#include "src/tint/lang/core/ir/let.h"
-#include "src/tint/lang/core/ir/load.h"
-#include "src/tint/lang/core/ir/load_vector_element.h"
#include "src/tint/lang/core/ir/loop.h"
#include "src/tint/lang/core/ir/multi_in_block.h"
#include "src/tint/lang/core/ir/next_iteration.h"
@@ -67,7 +61,6 @@
#include "src/tint/lang/core/type/type.h"
#include "src/tint/utils/ice/ice.h"
#include "src/tint/utils/macros/defer.h"
-#include "src/tint/utils/macros/scoped_assignment.h"
#include "src/tint/utils/rtti/switch.h"
#include "src/tint/utils/text/string.h"
#include "src/tint/utils/text/styled_text.h"
@@ -104,85 +97,16 @@
} // namespace
-StyledText Disassemble(const Module& mod) {
- return Disassembler{mod}.Disassemble();
+Disassembly::Disassembly(Disassembly&&) = default;
+
+Disassembly::Disassembly(const Module& mod, std::string_view file_name) : mod_(mod) {
+ Disassemble();
+ file_ = std::make_shared<Source::File>(std::string(file_name), Plain());
}
-Disassembler::Disassembler(const Module& mod) : mod_(mod) {}
+Disassembly::~Disassembly() = default;
-Disassembler::~Disassembler() = default;
-
-StyledText& Disassembler::Indent() {
- for (uint32_t i = 0; i < indent_size_; i++) {
- out_ << " ";
- }
- return out_;
-}
-
-void Disassembler::EmitLine() {
- out_ << "\n";
- current_output_line_ += 1;
- current_output_start_pos_ = static_cast<uint32_t>(out_.Length());
-}
-
-size_t Disassembler::IdOf(const Block* node) {
- TINT_ASSERT(node);
- return block_ids_.GetOrAdd(node, [&] { return block_ids_.Count(); });
-}
-
-std::string Disassembler::IdOf(const Value* value) {
- TINT_ASSERT(value);
- return value_ids_.GetOrAdd(value, [&] {
- if (auto sym = mod_.NameOf(value)) {
- if (ids_.Add(sym.Name())) {
- return sym.Name();
- }
- auto prefix = sym.Name() + "_";
- for (size_t i = 1;; i++) {
- auto name = prefix + std::to_string(i);
- if (ids_.Add(name)) {
- return name;
- }
- }
- }
- return std::to_string(value_ids_.Count());
- });
-}
-
-std::string Disassembler::NameOf(const If* inst) {
- if (!inst) {
- return "undef";
- }
-
- return if_names_.GetOrAdd(inst, [&] { return "if_" + std::to_string(if_names_.Count()); });
-}
-
-std::string Disassembler::NameOf(const Loop* inst) {
- if (!inst) {
- return "undef";
- }
-
- return loop_names_.GetOrAdd(inst,
- [&] { return "loop_" + std::to_string(loop_names_.Count()); });
-}
-
-std::string Disassembler::NameOf(const Switch* inst) {
- if (!inst) {
- return "undef";
- }
-
- return switch_names_.GetOrAdd(
- inst, [&] { return "switch_" + std::to_string(switch_names_.Count()); });
-}
-
-Source::Location Disassembler::MakeCurrentLocation() {
- return Source::Location{
- current_output_line_,
- static_cast<uint32_t>(out_.Length()) - current_output_start_pos_ + 1,
- };
-}
-
-const StyledText& Disassembler::Disassemble() {
+void Disassembly::Disassemble() {
TINT_DEFER(out_ << StylePlain);
out_.Clear();
out_ << StyleCode;
@@ -201,14 +125,33 @@
for (auto& func : mod_.functions) {
EmitFunction(func);
}
+}
+
+StyledText& Disassembly::Indent() {
+ for (uint32_t i = 0; i < indent_size_; i++) {
+ out_ << " ";
+ }
return out_;
}
-void Disassembler::EmitBlock(const Block* blk, std::string_view comment /* = "" */) {
+void Disassembly::EmitLine() {
+ out_ << "\n";
+ current_output_line_ += 1;
+ current_output_start_pos_ = static_cast<uint32_t>(out_.Length());
+}
+
+Source::Location Disassembly::MakeCurrentLocation() {
+ return Source::Location{
+ current_output_line_,
+ static_cast<uint32_t>(out_.Length()) - current_output_start_pos_ + 1,
+ };
+}
+
+void Disassembly::EmitBlock(const Block* blk, std::string_view comment /* = "" */) {
Indent();
SourceMarker sm(this);
- out_ << StyleLabel("$B", IdOf(blk));
+ out_ << NameOf(blk);
if (auto* merge = blk->As<MultiInBlock>()) {
if (!merge->Params().IsEmpty()) {
out_ << " (";
@@ -216,9 +159,12 @@
if (p != merge->Params().Front()) {
out_ << ", ";
}
- SourceMarker psm(this);
- EmitValue(p);
- psm.Store(p);
+ {
+ SourceMarker psm(this);
+ EmitValue(p);
+ psm.Store(p);
+ }
+ out_ << ":" << StyleType(p->Type()->FriendlyName());
}
out_ << ")";
}
@@ -243,12 +189,12 @@
EmitLine();
}
-void Disassembler::EmitBindingPoint(BindingPoint p) {
+void Disassembly::EmitBindingPoint(BindingPoint p) {
out_ << StyleAttribute("@binding_point") << "(" << StyleLiteral(p.group) << ", "
<< StyleLiteral(p.binding) << ")";
}
-void Disassembler::EmitLocation(Location loc) {
+void Disassembly::EmitLocation(Location loc) {
out_ << StyleAttribute("@location") << "(" << loc.value << ")";
if (loc.interpolation.has_value()) {
out_ << ", " << StyleAttribute("@interpolate") << "(";
@@ -261,7 +207,7 @@
}
}
-void Disassembler::EmitParamAttributes(const FunctionParam* p) {
+void Disassembly::EmitParamAttributes(const FunctionParam* p) {
if (!p->Invariant() && !p->Location().has_value() && !p->BindingPoint().has_value() &&
!p->Builtin().has_value()) {
return;
@@ -298,7 +244,7 @@
out_ << "]";
}
-void Disassembler::EmitReturnAttributes(const Function* func) {
+void Disassembly::EmitReturnAttributes(const Function* func) {
if (!func->ReturnInvariant() && !func->ReturnLocation().has_value() &&
!func->ReturnBuiltin().has_value()) {
return;
@@ -330,13 +276,13 @@
out_ << "]";
}
-void Disassembler::EmitFunction(const Function* func) {
+void Disassembly::EmitFunction(const Function* func) {
in_function_ = true;
- std::string fn_id = IdOf(func);
+ auto fn_id = NameOf(func);
{
SourceMarker sm(this);
- Indent() << StyleFunction("%", fn_id);
+ Indent() << fn_id;
sm.Store(func);
}
out_ << " =";
@@ -357,7 +303,7 @@
out_ << ", ";
}
SourceMarker sm(this);
- out_ << StyleVariable("%", IdOf(p)) << ":" << StyleType(p->Type()->FriendlyName());
+ out_ << NameOf(p) << ":" << StyleType(p->Type()->FriendlyName());
sm.Store(p);
EmitParamAttributes(p);
@@ -371,15 +317,15 @@
{ // Add a comment if the function IDs or parameter IDs doesn't match their name
Vector<std::string, 4> names;
if (auto name = mod_.NameOf(func); name.IsValid()) {
- if (name.NameView() != fn_id) {
- names.Push("%" + std::string(fn_id) + ": '" + name.Name() + "'");
+ if ("%" + name.Name() != fn_id.Plain()) {
+ names.Push(fn_id.Plain() + ": '" + name.Name() + "'");
}
}
for (auto* p : func->Params()) {
if (auto name = mod_.NameOf(p); name.IsValid()) {
- auto id = IdOf(p);
- if (name.NameView() != id) {
- names.Push("%" + std::string(id) + ": '" + name.Name() + "'");
+ auto id = NameOf(p);
+ if ("%" + name.Name() != id.Plain()) {
+ names.Push(id.Plain() + ": '" + name.Name() + "'");
}
}
}
@@ -398,27 +344,24 @@
EmitLine();
}
-void Disassembler::EmitValueWithType(const Instruction* val) {
+void Disassembly::EmitValueWithType(const Instruction* val) {
SourceMarker sm(this);
- if (val->Result(0)) {
- EmitValueWithType(val->Result(0));
- } else {
- out_ << "undef";
- }
+ EmitValueWithType(val->Result(0));
sm.StoreResult(IndexedValue{val, 0});
}
-void Disassembler::EmitValueWithType(const Value* val) {
- if (!val) {
- out_ << "undef";
- return;
- }
-
+void Disassembly::EmitValueWithType(const Value* val) {
EmitValue(val);
- out_ << ":" << StyleType(val->Type()->FriendlyName());
+ if (val) {
+ out_ << ":" << StyleType(val->Type()->FriendlyName());
+ }
}
-void Disassembler::EmitValue(const Value* val) {
+void Disassembly::EmitValue(const Value* val) {
+ if (!val) {
+ out_ << StyleLiteral("undef");
+ return;
+ }
tint::Switch(
val,
[&](const ir::Constant* constant) {
@@ -467,28 +410,16 @@
};
emit(constant->Value());
},
- [&](const ir::InstructionResult* rv) { out_ << StyleVariable("%", IdOf(rv)); },
- [&](const ir::BlockParam* p) {
- out_ << StyleVariable("%", IdOf(p)) << ":" << StyleType(p->Type()->FriendlyName());
- },
- [&](const ir::FunctionParam* p) { out_ << StyleVariable("%", IdOf(p)); },
- [&](const ir::Function* f) { out_ << StyleVariable("%", IdOf(f)); },
- [&](Default) {
- if (val == nullptr) {
- out_ << StyleVariable("undef");
- } else {
- out_ << StyleError("unknown value: ", val->TypeInfo().name);
- }
- });
+ [&](Default) { out_ << NameOf(val); });
}
-void Disassembler::EmitInstructionName(const Instruction* inst) {
+void Disassembly::EmitInstructionName(const Instruction* inst) {
SourceMarker sm(this);
out_ << StyleInstruction(inst->FriendlyName());
sm.Store(inst);
}
-void Disassembler::EmitInstruction(const Instruction* inst) {
+void Disassembly::EmitInstruction(const Instruction* inst) {
TINT_DEFER(EmitLine());
if (!inst->Alive()) {
@@ -604,9 +535,9 @@
for (auto* result : inst->Results()) {
if (result) {
if (auto name = mod_.NameOf(result); name.IsValid()) {
- auto id = IdOf(result);
- if (name.NameView() != id) {
- names.Push("%" + std::string(id) + ": '" + name.Name() + "'");
+ auto id = NameOf(result).Plain();
+ if ("%" + name.Name() != id) {
+ names.Push(id + ": '" + name.Name() + "'");
}
}
}
@@ -617,13 +548,13 @@
}
}
-void Disassembler::EmitOperand(const Instruction* inst, size_t index) {
+void Disassembly::EmitOperand(const Instruction* inst, size_t index) {
SourceMarker marker(this);
EmitValue(inst->Operands()[index]);
marker.Store(IndexedValue{inst, static_cast<uint32_t>(index)});
}
-void Disassembler::EmitOperandList(const Instruction* inst, size_t start_index /* = 0 */) {
+void Disassembly::EmitOperandList(const Instruction* inst, size_t start_index /* = 0 */) {
for (size_t i = start_index, n = inst->Operands().Length(); i < n; i++) {
if (i != start_index) {
out_ << ", ";
@@ -632,7 +563,7 @@
}
}
-void Disassembler::EmitIf(const If* if_) {
+void Disassembly::EmitIf(const If* if_) {
SourceMarker sm(this);
if (auto results = if_->Results(); !results.IsEmpty()) {
for (size_t i = 0; i < results.Length(); ++i) {
@@ -650,9 +581,9 @@
bool has_false = !if_->False()->IsEmpty();
- out_ << " [" << StyleKeyword("t") << ": " << StyleLabel("$B", IdOf(if_->True()));
+ out_ << " [" << StyleKeyword("t") << ": " << NameOf(if_->True());
if (has_false) {
- out_ << ", " << StyleKeyword("f") << ": " << StyleLabel("$B", IdOf(if_->False()));
+ out_ << ", " << StyleKeyword("f") << ": " << NameOf(if_->False());
}
out_ << "]";
sm.Store(if_);
@@ -683,7 +614,7 @@
out_ << "}";
}
-void Disassembler::EmitLoop(const Loop* l) {
+void Disassembly::EmitLoop(const Loop* l) {
SourceMarker sm(this);
if (auto results = l->Results(); !results.IsEmpty()) {
for (size_t i = 0; i < results.Length(); ++i) {
@@ -699,17 +630,15 @@
out_ << StyleInstruction("loop") << " [";
if (!l->Initializer()->IsEmpty()) {
- out_ << StyleKeyword("i") << ": "
- << StyleLabel("$B", std::to_string(IdOf(l->Initializer())));
+ out_ << StyleKeyword("i") << ": " << NameOf(l->Initializer());
out_ << ", ";
}
- out_ << StyleKeyword("b") << ": " << StyleLabel("$B", std::to_string(IdOf(l->Body())));
+ out_ << StyleKeyword("b") << ": " << NameOf(l->Body());
if (!l->Continuing()->IsEmpty()) {
out_ << ", ";
- out_ << StyleKeyword("c") << ": "
- << StyleLabel("$B", std::to_string(IdOf(l->Continuing())));
+ out_ << StyleKeyword("c") << ": " << NameOf(l->Continuing());
}
out_ << "]";
@@ -738,7 +667,7 @@
out_ << "}";
}
-void Disassembler::EmitSwitch(const Switch* s) {
+void Disassembly::EmitSwitch(const Switch* s) {
SourceMarker sm(this);
if (auto results = s->Results(); !results.IsEmpty()) {
for (size_t i = 0; i < results.Length(); ++i) {
@@ -770,7 +699,7 @@
EmitValue(selector.val);
}
}
- out_ << ", " << StyleLabel("$B", IdOf(c.block)) << ")";
+ out_ << ", " << NameOf(c.block) << ")";
}
out_ << "]";
sm.Store(s);
@@ -787,7 +716,7 @@
out_ << "}";
}
-void Disassembler::EmitTerminator(const Terminator* b) {
+void Disassembly::EmitTerminator(const Terminator* b) {
SourceMarker sm(this);
size_t args_offset = 0;
tint::Switch(
@@ -835,11 +764,11 @@
b, //
[&](const ir::BreakIf* bi) {
out_ << " "
- << StyleComment("# -> [t: exit_loop ", NameOf(bi->Loop()), ", f: $B",
- IdOf(bi->Loop()->Body()), "]");
+ << StyleComment("# -> [t: exit_loop ", NameOf(bi->Loop()),
+ ", f: ", NameOf(bi->Loop()->Body()), "]");
},
[&](const ir::Continue* c) {
- out_ << " " << StyleComment("# -> $B", IdOf(c->Loop()->Continuing()));
+ out_ << " " << StyleComment("# -> ", NameOf(c->Loop()->Continuing()));
}, //
[&](const ir::ExitIf* e) { out_ << " " << StyleComment("# ", NameOf(e->If())); }, //
[&](const ir::ExitSwitch* e) {
@@ -847,11 +776,11 @@
}, //
[&](const ir::ExitLoop* e) { out_ << " " << StyleComment("# ", NameOf(e->Loop())); }, //
[&](const ir::NextIteration* ni) {
- out_ << " " << StyleComment("# -> $B", IdOf(ni->Loop()->Body()));
+ out_ << " " << StyleComment("# -> ", NameOf(ni->Loop()->Body()));
});
}
-void Disassembler::EmitBinary(const Binary* b) {
+void Disassembly::EmitBinary(const Binary* b) {
SourceMarker sm(this);
EmitValueWithType(b);
out_ << " = ";
@@ -917,7 +846,7 @@
sm.Store(b);
}
-void Disassembler::EmitUnary(const Unary* u) {
+void Disassembly::EmitUnary(const Unary* u) {
SourceMarker sm(this);
EmitValueWithType(u);
out_ << " = ";
@@ -944,7 +873,7 @@
sm.Store(u);
}
-void Disassembler::EmitStructDecl(const core::type::Struct* str) {
+void Disassembly::EmitStructDecl(const core::type::Struct* str) {
out_ << StyleType(str->Name().Name()) << " = " << StyleKeyword("struct") << " "
<< StyleAttribute("@align") << "(" << StyleLiteral(str->Align()) << ")";
if (str->StructFlags().Contains(core::type::StructFlag::kBlock)) {
@@ -982,4 +911,64 @@
EmitLine();
}
+StyledText Disassembly::NameOf(const Block* node) {
+ TINT_ASSERT(node);
+ auto id = block_ids_.GetOrAdd(node, [&] { return block_ids_.Count(); });
+ return StyledText{} << StyleLabel("$B", id);
+}
+
+StyledText Disassembly::NameOf(const Value* value) {
+ TINT_ASSERT(value);
+ auto id = value_ids_.GetOrAdd(value, [&] {
+ if (auto sym = mod_.NameOf(value)) {
+ if (ids_.Add(sym.Name())) {
+ return sym.Name();
+ }
+ auto prefix = sym.Name() + "_";
+ for (size_t i = 1;; i++) {
+ auto name = prefix + std::to_string(i);
+ if (ids_.Add(name)) {
+ return name;
+ }
+ }
+ }
+ return std::to_string(value_ids_.Count());
+ });
+
+ auto style = tint::Switch(
+ value, //
+ [&](const Function*) { return StyleFunction; }, //
+ [&](const InstructionResult*) { return StyleVariable; });
+ return StyledText{} << style("%", id);
+}
+
+StyledText Disassembly::NameOf(const If* inst) {
+ if (!inst) {
+ return StyledText{} << StyleError("undef");
+ }
+
+ auto name = if_names_.GetOrAdd(inst, [&] { return "if_" + std::to_string(if_names_.Count()); });
+ return StyledText{} << StyleInstruction(name);
+}
+
+StyledText Disassembly::NameOf(const Loop* inst) {
+ if (!inst) {
+ return StyledText{} << StyleError("undef");
+ }
+
+ auto name =
+ loop_names_.GetOrAdd(inst, [&] { return "loop_" + std::to_string(loop_names_.Count()); });
+ return StyledText{} << StyleInstruction(name);
+}
+
+StyledText Disassembly::NameOf(const Switch* inst) {
+ if (!inst) {
+ return StyledText{} << StyleError("undef");
+ }
+
+ auto name = switch_names_.GetOrAdd(
+ inst, [&] { return "switch_" + std::to_string(switch_names_.Count()); });
+ return StyledText{} << StyleInstruction(name);
+}
+
} // namespace tint::core::ir
diff --git a/src/tint/lang/core/ir/disassembler.h b/src/tint/lang/core/ir/disassembly.h
similarity index 78%
rename from src/tint/lang/core/ir/disassembler.h
rename to src/tint/lang/core/ir/disassembly.h
index df20534..677b386 100644
--- a/src/tint/lang/core/ir/disassembler.h
+++ b/src/tint/lang/core/ir/disassembly.h
@@ -25,10 +25,12 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#ifndef SRC_TINT_LANG_CORE_IR_DISASSEMBLER_H_
-#define SRC_TINT_LANG_CORE_IR_DISASSEMBLER_H_
+#ifndef SRC_TINT_LANG_CORE_IR_DISASSEMBLY_H_
+#define SRC_TINT_LANG_CORE_IR_DISASSEMBLY_H_
+#include <memory>
#include <string>
+#include <string_view>
#include "src/tint/lang/core/ir/binary.h"
#include "src/tint/lang/core/ir/block.h"
@@ -40,7 +42,6 @@
#include "src/tint/lang/core/ir/unary.h"
#include "src/tint/utils/containers/hashmap.h"
#include "src/tint/utils/containers/hashset.h"
-#include "src/tint/utils/text/string_stream.h"
#include "src/tint/utils/text/styled_text.h"
// Forward declarations.
@@ -50,12 +51,8 @@
namespace tint::core::ir {
-/// @returns the disassembly for the module @p mod
-/// @param mod the module to disassemble
-StyledText Disassemble(const Module& mod);
-
-/// Helper class to disassemble the IR
-class Disassembler {
+/// Disassembly holds the disassembly of an IR module.
+class Disassembly {
public:
/// A reference to an instruction's operand or result.
struct IndexedValue {
@@ -75,32 +72,63 @@
}
};
- /// Constructor
- /// @param mod the module
- explicit Disassembler(const Module& mod);
- ~Disassembler();
+ /// Constructor.
+ /// Performs the disassembly of the module @p mod, constructing a Source::File with the name @p
+ /// file_name.
+ /// @param mod the module to disassemble
+ Disassembly(const Module& mod, std::string_view file_name);
- /// Returns the module as a styled text string
+ /// Move constructor
+ Disassembly(Disassembly&&);
+
+ /// Destructor
+ ~Disassembly();
+
/// @returns the string representation of the module
- const StyledText& Disassemble();
+ const StyledText& Text() const { return out_; }
+
+ /// @returns the string representation of the module as plain-text
+ std::string Plain() const { return out_.Plain(); }
+
+ /// @returns the disassembly file
+ const std::shared_ptr<Source::File>& File() const { return file_; }
+
+ /// @returns the disassembled name for the Block @p blk
+ StyledText NameOf(const Block* blk);
+
+ /// @returns the disassembled name for the Value @p node
+ StyledText NameOf(const Value* node);
+
+ /// @returns the disassembled name for the If @p inst
+ StyledText NameOf(const If* inst);
+
+ /// @returns the disassembled name for the Loop @p inst
+ StyledText NameOf(const Loop* inst);
+
+ /// @returns the disassembled name for the Switch @p inst
+ StyledText NameOf(const Switch* inst);
/// @param inst the instruction to retrieve
/// @returns the source for the instruction
- Source InstructionSource(const Instruction* inst) {
+ Source InstructionSource(const Instruction* inst) const {
return instruction_to_src_.GetOr(inst, Source{});
}
/// @param operand the operand to retrieve
/// @returns the source for the operand
- Source OperandSource(IndexedValue operand) { return operand_to_src_.GetOr(operand, Source{}); }
+ Source OperandSource(IndexedValue operand) const {
+ return operand_to_src_.GetOr(operand, Source{});
+ }
/// @param result the result to retrieve
/// @returns the source for the result
- Source ResultSource(IndexedValue result) { return result_to_src_.GetOr(result, Source{}); }
+ Source ResultSource(IndexedValue result) const {
+ return result_to_src_.GetOr(result, Source{});
+ }
/// @param blk the block to retrieve
/// @returns the source for the block
- Source BlockSource(const Block* blk) { return block_to_src_.GetOr(blk, Source{}); }
+ Source BlockSource(const Block* blk) const { return block_to_src_.GetOr(blk, Source{}); }
/// @param param the block parameter to retrieve
/// @returns the source for the parameter
@@ -118,6 +146,10 @@
return function_param_to_src_.GetOr(param, Source{});
}
+ private:
+ /// Performs the disassembling of the module.
+ void Disassemble();
+
/// Stores the given @p src location for @p inst instruction
/// @param inst the instruction to store
/// @param src the source location
@@ -158,10 +190,9 @@
/// @returns the source location for the current emission location
Source::Location MakeCurrentLocation();
- private:
class SourceMarker {
public:
- explicit SourceMarker(Disassembler* d) : dis_(d), begin_(dis_->MakeCurrentLocation()) {}
+ explicit SourceMarker(Disassembly* d) : dis_(d), begin_(dis_->MakeCurrentLocation()) {}
~SourceMarker() = default;
void Store(const Instruction* inst) { dis_->SetSource(inst, MakeSource()); }
@@ -183,18 +214,12 @@
}
private:
- Disassembler* dis_ = nullptr;
+ Disassembly* dis_ = nullptr;
Source::Location begin_;
};
StyledText& Indent();
- size_t IdOf(const Block* blk);
- std::string IdOf(const Value* node);
- std::string NameOf(const If* inst);
- std::string NameOf(const Loop* inst);
- std::string NameOf(const Switch* inst);
-
void EmitBlock(const Block* blk, std::string_view comment = "");
void EmitFunction(const Function* func);
void EmitParamAttributes(const FunctionParam* p);
@@ -219,9 +244,7 @@
const Module& mod_;
StyledText out_;
- Hashmap<const Block*, size_t, 32> block_ids_;
- Hashmap<const Value*, std::string, 32> value_ids_;
- Hashset<std::string, 32> ids_;
+ std::shared_ptr<Source::File> file_;
uint32_t indent_size_ = 0;
bool in_function_ = false;
@@ -235,11 +258,21 @@
Hashmap<IndexedValue, Source, 8> result_to_src_;
Hashmap<const Function*, Source, 8> function_to_src_;
Hashmap<const FunctionParam*, Source, 8> function_param_to_src_;
+
+ // Names / IDs
+ Hashmap<const Block*, size_t, 32> block_ids_;
+ Hashmap<const Value*, std::string, 32> value_ids_;
Hashmap<const If*, std::string, 8> if_names_;
Hashmap<const Loop*, std::string, 8> loop_names_;
Hashmap<const Switch*, std::string, 8> switch_names_;
+ Hashset<std::string, 32> ids_;
};
+/// @returns the disassembly for the module @p mod, using the file name @p file_name
+inline Disassembly Disassemble(const Module& mod, std::string_view file_name = "") {
+ return Disassembly(mod, file_name);
+}
+
} // namespace tint::core::ir
-#endif // SRC_TINT_LANG_CORE_IR_DISASSEMBLER_H_
+#endif // SRC_TINT_LANG_CORE_IR_DISASSEMBLY_H_
diff --git a/src/tint/lang/core/ir/ice.h b/src/tint/lang/core/ir/ice.h
index 6487cc3..cdc3e09 100644
--- a/src/tint/lang/core/ir/ice.h
+++ b/src/tint/lang/core/ir/ice.h
@@ -28,10 +28,9 @@
#ifndef SRC_TINT_LANG_CORE_IR_ICE_H_
#define SRC_TINT_LANG_CORE_IR_ICE_H_
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
/// Emit an ICE message with the disassembly of `mod` attached.
-#define TINT_IR_ICE(mod) \
- TINT_ICE() << tint::core::ir::Disassembler{mod}.Disassemble().Plain() << "\n"
+#define TINT_IR_ICE(mod) TINT_ICE() << tint::core::ir::Disassemble(mod).Plain() << "\n"
#endif // SRC_TINT_LANG_CORE_IR_ICE_H_
diff --git a/src/tint/lang/core/ir/transform/helper_test.h b/src/tint/lang/core/ir/transform/helper_test.h
index ef8e375..8daa4d0 100644
--- a/src/tint/lang/core/ir/transform/helper_test.h
+++ b/src/tint/lang/core/ir/transform/helper_test.h
@@ -35,7 +35,7 @@
#include "gtest/gtest.h"
#include "src/tint/lang/core/ir/builder.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/validator.h"
#include "src/tint/utils/containers/enum_set.h"
diff --git a/src/tint/lang/core/ir/transform/preserve_padding_test.cc b/src/tint/lang/core/ir/transform/preserve_padding_test.cc
index 83f5942..188029c 100644
--- a/src/tint/lang/core/ir/transform/preserve_padding_test.cc
+++ b/src/tint/lang/core/ir/transform/preserve_padding_test.cc
@@ -661,19 +661,19 @@
next_iteration 0u # -> $B5
}
$B5 (%idx:u32): { # body
- %9:bool = gte %idx:u32, 4u
+ %9:bool = gte %idx, 4u
if %9 [t: $B7] { # if_1
$B7: { # true
exit_loop # loop_1
}
}
- %10:ptr<storage, MyStruct, read_write> = access %target, %idx:u32
- %11:MyStruct = access %value_param, %idx:u32
+ %10:ptr<storage, MyStruct, read_write> = access %target, %idx
+ %11:MyStruct = access %value_param, %idx
%12:void = call %tint_store_and_preserve_padding_1, %10, %11
continue # -> $B6
}
$B6: { # continuing
- %14:u32 = add %idx:u32, 1u
+ %14:u32 = add %idx, 1u
next_iteration %14 # -> $B5
}
}
@@ -893,19 +893,19 @@
next_iteration 0u # -> $B5
}
$B5 (%idx:u32): { # body
- %9:bool = gte %idx:u32, 4u
+ %9:bool = gte %idx, 4u
if %9 [t: $B7] { # if_1
$B7: { # true
exit_loop # loop_1
}
}
- %10:ptr<storage, mat3x3<f32>, read_write> = access %target, %idx:u32
- %11:mat3x3<f32> = access %value_param, %idx:u32
+ %10:ptr<storage, mat3x3<f32>, read_write> = access %target, %idx
+ %11:mat3x3<f32> = access %value_param, %idx
%12:void = call %tint_store_and_preserve_padding_1, %10, %11
continue # -> $B6
}
$B6: { # continuing
- %14:u32 = add %idx:u32, 1u
+ %14:u32 = add %idx, 1u
next_iteration %14 # -> $B5
}
}
@@ -980,19 +980,19 @@
next_iteration 0u # -> $B5
}
$B5 (%idx:u32): { # body
- %9:bool = gte %idx:u32, 4u
+ %9:bool = gte %idx, 4u
if %9 [t: $B7] { # if_1
$B7: { # true
exit_loop # loop_1
}
}
- %10:ptr<storage, vec3<f32>, read_write> = access %target, %idx:u32
- %11:vec3<f32> = access %value_param, %idx:u32
+ %10:ptr<storage, vec3<f32>, read_write> = access %target, %idx
+ %11:vec3<f32> = access %value_param, %idx
store %10, %11
continue # -> $B6
}
$B6: { # continuing
- %12:u32 = add %idx:u32, 1u
+ %12:u32 = add %idx, 1u
next_iteration %12 # -> $B5
}
}
@@ -1095,19 +1095,19 @@
next_iteration 0u # -> $B5
}
$B5 (%idx:u32): { # body
- %9:bool = gte %idx:u32, 3u
+ %9:bool = gte %idx, 3u
if %9 [t: $B7] { # if_1
$B7: { # true
exit_loop # loop_1
}
}
- %10:ptr<storage, Outer, read_write> = access %target, %idx:u32
- %11:Outer = access %value_param, %idx:u32
+ %10:ptr<storage, Outer, read_write> = access %target, %idx
+ %11:Outer = access %value_param, %idx
%12:void = call %tint_store_and_preserve_padding_1, %10, %11
continue # -> $B6
}
$B6: { # continuing
- %14:u32 = add %idx:u32, 1u
+ %14:u32 = add %idx, 1u
next_iteration %14 # -> $B5
}
}
@@ -1155,19 +1155,19 @@
next_iteration 0u # -> $B12
}
$B12 (%idx_1:u32): { # body
- %46:bool = gte %idx_1:u32, 4u
+ %46:bool = gte %idx_1, 4u
if %46 [t: $B14] { # if_2
$B14: { # true
exit_loop # loop_2
}
}
- %47:ptr<storage, vec3<f32>, read_write> = access %target_3, %idx_1:u32
- %48:vec3<f32> = access %value_param_3, %idx_1:u32
+ %47:ptr<storage, vec3<f32>, read_write> = access %target_3, %idx_1
+ %48:vec3<f32> = access %value_param_3, %idx_1
store %47, %48
continue # -> $B13
}
$B13: { # continuing
- %49:u32 = add %idx_1:u32, 1u
+ %49:u32 = add %idx_1, 1u
next_iteration %49 # -> $B12
}
}
@@ -1195,19 +1195,19 @@
next_iteration 0u # -> $B18
}
$B18 (%idx_2:u32): { # body
- %61:bool = gte %idx_2:u32, 4u
+ %61:bool = gte %idx_2, 4u
if %61 [t: $B20] { # if_3
$B20: { # true
exit_loop # loop_3
}
}
- %62:ptr<storage, Inner, read_write> = access %target_5, %idx_2:u32
- %63:Inner = access %value_param_5, %idx_2:u32
+ %62:ptr<storage, Inner, read_write> = access %target_5, %idx_2
+ %63:Inner = access %value_param_5, %idx_2
%64:void = call %tint_store_and_preserve_padding_2, %62, %63
continue # -> $B19
}
$B19: { # continuing
- %65:u32 = add %idx_2:u32, 1u
+ %65:u32 = add %idx_2, 1u
next_iteration %65 # -> $B18
}
}
diff --git a/src/tint/lang/core/ir/transform/std140_test.cc b/src/tint/lang/core/ir/transform/std140_test.cc
index 4b819bb..dd55108 100644
--- a/src/tint/lang/core/ir/transform/std140_test.cc
+++ b/src/tint/lang/core/ir/transform/std140_test.cc
@@ -565,20 +565,20 @@
next_iteration 0u # -> $B5
}
$B5 (%idx:u32): { # body
- %10:bool = gte %idx:u32, 4u
+ %10:bool = gte %idx, 4u
if %10 [t: $B7] { # if_1
$B7: { # true
exit_loop # loop_1
}
}
- %11:ptr<function, Inner, read_write> = access %8, %idx:u32
- %12:Inner_std140 = access %7, %idx:u32
+ %11:ptr<function, Inner, read_write> = access %8, %idx
+ %12:Inner_std140 = access %7, %idx
%13:Inner = call %convert_Inner, %12
store %11, %13
continue # -> $B6
}
$B6: { # continuing
- %15:u32 = add %idx:u32, 1u
+ %15:u32 = add %idx, 1u
next_iteration %15 # -> $B5
}
}
@@ -906,20 +906,20 @@
next_iteration 0u # -> $B4
}
$B4 (%idx:u32): { # body
- %16:bool = gte %idx:u32, 4u
+ %16:bool = gte %idx, 4u
if %16 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %17:ptr<function, Inner, read_write> = access %14, %idx:u32
- %18:Inner_std140 = access %13, %idx:u32
+ %17:ptr<function, Inner, read_write> = access %14, %idx
+ %18:Inner_std140 = access %13, %idx
%19:Inner = call %convert_Inner, %18
store %17, %19
continue # -> $B5
}
$B5: { # continuing
- %21:u32 = add %idx:u32, 1u
+ %21:u32 = add %idx, 1u
next_iteration %21 # -> $B4
}
}
@@ -1081,20 +1081,20 @@
next_iteration 0u # -> $B4
}
$B4 (%idx:u32): { # body
- %16:bool = gte %idx:u32, 4u
+ %16:bool = gte %idx, 4u
if %16 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %17:ptr<function, Inner, read_write> = access %14, %idx:u32
- %18:Inner_std140 = access %13, %idx:u32
+ %17:ptr<function, Inner, read_write> = access %14, %idx
+ %18:Inner_std140 = access %13, %idx
%19:Inner = call %convert_Inner, %18
store %17, %19
continue # -> $B5
}
$B5: { # continuing
- %21:u32 = add %idx:u32, 1u
+ %21:u32 = add %idx, 1u
next_iteration %21 # -> $B4
}
}
@@ -1255,20 +1255,20 @@
next_iteration 0u # -> $B4
}
$B4 (%idx:u32): { # body
- %19:bool = gte %idx:u32, 4u
+ %19:bool = gte %idx, 4u
if %19 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %20:ptr<function, Inner, read_write> = access %17, %idx:u32
- %21:Inner_std140 = access %16, %idx:u32
+ %20:ptr<function, Inner, read_write> = access %17, %idx
+ %21:Inner_std140 = access %16, %idx
%22:Inner = call %convert_Inner, %21
store %20, %22
continue # -> $B5
}
$B5: { # continuing
- %24:u32 = add %idx:u32, 1u
+ %24:u32 = add %idx, 1u
next_iteration %24 # -> $B4
}
}
diff --git a/src/tint/lang/core/ir/transform/zero_init_workgroup_memory_test.cc b/src/tint/lang/core/ir/transform/zero_init_workgroup_memory_test.cc
index 0e9c0b1..bf1a82b 100644
--- a/src/tint/lang/core/ir/transform/zero_init_workgroup_memory_test.cc
+++ b/src/tint/lang/core/ir/transform/zero_init_workgroup_memory_test.cc
@@ -480,18 +480,18 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 4u
+ %5:bool = gte %idx, 4u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:ptr<workgroup, i32, read_write> = access %wgvar, %idx:u32
+ %6:ptr<workgroup, i32, read_write> = access %wgvar, %idx
store %6, 0i
continue # -> $B5
}
$B5: { # continuing
- %7:u32 = add %idx:u32, 66u
+ %7:u32 = add %idx, 66u
next_iteration %7 # -> $B4
}
}
@@ -542,20 +542,20 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 35u
+ %5:bool = gte %idx, 35u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:u32 = mod %idx:u32, 5u
- %7:u32 = div %idx:u32, 5u
+ %6:u32 = mod %idx, 5u
+ %7:u32 = div %idx, 5u
%8:ptr<workgroup, u32, read_write> = access %wgvar, %7, %6
store %8, 0u
continue # -> $B5
}
$B5: { # continuing
- %9:u32 = add %idx:u32, 66u
+ %9:u32 = add %idx, 66u
next_iteration %9 # -> $B4
}
}
@@ -606,22 +606,22 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 105u
+ %5:bool = gte %idx, 105u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:u32 = mod %idx:u32, 7u
- %7:u32 = div %idx:u32, 7u
+ %6:u32 = mod %idx, 7u
+ %7:u32 = div %idx, 7u
%8:u32 = mod %7, 5u
- %9:u32 = div %idx:u32, 35u
+ %9:u32 = div %idx, 35u
%10:ptr<workgroup, i32, read_write> = access %wgvar, %9, %8, %6
store %10, 0i
continue # -> $B5
}
$B5: { # continuing
- %11:u32 = add %idx:u32, 1u
+ %11:u32 = add %idx, 1u
next_iteration %11 # -> $B4
}
}
@@ -672,20 +672,20 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 15u
+ %5:bool = gte %idx, 15u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:u32 = mod %idx:u32, 5u
- %7:u32 = div %idx:u32, 5u
+ %6:u32 = mod %idx, 5u
+ %7:u32 = div %idx, 5u
%8:ptr<workgroup, i32, read_write> = access %wgvar, %7, %6, 0u
store %8, 0i
continue # -> $B5
}
$B5: { # continuing
- %9:u32 = add %idx:u32, 1u
+ %9:u32 = add %idx, 1u
next_iteration %9 # -> $B4
}
}
@@ -736,20 +736,20 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 15u
+ %5:bool = gte %idx, 15u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:u32 = mod %idx:u32, 3u
- %7:u32 = div %idx:u32, 3u
+ %6:u32 = mod %idx, 3u
+ %7:u32 = div %idx, 3u
%8:ptr<workgroup, i32, read_write> = access %wgvar, %7, 0u, %6
store %8, 0i
continue # -> $B5
}
$B5: { # continuing
- %9:u32 = add %idx:u32, 1u
+ %9:u32 = add %idx, 1u
next_iteration %9 # -> $B4
}
}
@@ -800,20 +800,20 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 15u
+ %5:bool = gte %idx, 15u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:u32 = mod %idx:u32, 3u
- %7:u32 = div %idx:u32, 3u
+ %6:u32 = mod %idx, 3u
+ %7:u32 = div %idx, 3u
%8:ptr<workgroup, i32, read_write> = access %wgvar, 0u, %7, %6
store %8, 0i
continue # -> $B5
}
$B5: { # continuing
- %9:u32 = add %idx:u32, 1u
+ %9:u32 = add %idx, 1u
next_iteration %9 # -> $B4
}
}
@@ -1179,20 +1179,20 @@
next_iteration %tint_local_index # -> $B4
}
$B4 (%idx:u32): { # body
- %5:bool = gte %idx:u32, 7u
+ %5:bool = gte %idx, 7u
if %5 [t: $B6] { # if_1
$B6: { # true
exit_loop # loop_1
}
}
- %6:ptr<workgroup, f32, read_write> = access %wgvar, %idx:u32, 0u
+ %6:ptr<workgroup, f32, read_write> = access %wgvar, %idx, 0u
store %6, 0.0f
- %7:ptr<workgroup, bool, read_write> = access %wgvar, %idx:u32, 2u
+ %7:ptr<workgroup, bool, read_write> = access %wgvar, %idx, 2u
store %7, false
continue # -> $B5
}
$B5: { # continuing
- %8:u32 = add %idx:u32, 42u
+ %8:u32 = add %idx, 42u
next_iteration %8 # -> $B4
}
}
@@ -1201,24 +1201,24 @@
next_iteration %tint_local_index # -> $B8
}
$B8 (%idx_1:u32): { # body
- %10:bool = gte %idx_1:u32, 91u
+ %10:bool = gte %idx_1, 91u
if %10 [t: $B10] { # if_2
$B10: { # true
exit_loop # loop_2
}
}
- %11:u32 = mod %idx_1:u32, 13u
- %12:u32 = div %idx_1:u32, 13u
+ %11:u32 = mod %idx_1, 13u
+ %12:u32 = div %idx_1, 13u
%13:ptr<workgroup, i32, read_write> = access %wgvar, %12, 1u, %11, 0u
store %13, 0i
- %14:u32 = mod %idx_1:u32, 13u
- %15:u32 = div %idx_1:u32, 13u
+ %14:u32 = mod %idx_1, 13u
+ %15:u32 = div %idx_1, 13u
%16:ptr<workgroup, atomic<u32>, read_write> = access %wgvar, %15, 1u, %14, 1u
%17:void = atomicStore %16, 0u
continue # -> $B9
}
$B9: { # continuing
- %18:u32 = add %idx_1:u32, 42u
+ %18:u32 = add %idx_1, 42u
next_iteration %18 # -> $B8
}
}
@@ -1286,18 +1286,18 @@
next_iteration %tint_local_index # -> $B5
}
$B5 (%idx:u32): { # body
- %8:bool = gte %idx:u32, 4u
+ %8:bool = gte %idx, 4u
if %8 [t: $B7] { # if_2
$B7: { # true
exit_loop # loop_1
}
}
- %9:ptr<workgroup, i32, read_write> = access %var_b, %idx:u32
+ %9:ptr<workgroup, i32, read_write> = access %var_b, %idx
store %9, 0i
continue # -> $B6
}
$B6: { # continuing
- %10:u32 = add %idx:u32, 66u
+ %10:u32 = add %idx, 66u
next_iteration %10 # -> $B5
}
}
@@ -1306,20 +1306,20 @@
next_iteration %tint_local_index # -> $B9
}
$B9 (%idx_1:u32): { # body
- %12:bool = gte %idx_1:u32, 35u
+ %12:bool = gte %idx_1, 35u
if %12 [t: $B11] { # if_3
$B11: { # true
exit_loop # loop_2
}
}
- %13:u32 = mod %idx_1:u32, 5u
- %14:u32 = div %idx_1:u32, 5u
+ %13:u32 = mod %idx_1, 5u
+ %14:u32 = div %idx_1, 5u
%15:ptr<workgroup, u32, read_write> = access %var_c, %14, %13
store %15, 0u
continue # -> $B10
}
$B10: { # continuing
- %16:u32 = add %idx_1:u32, 66u
+ %16:u32 = add %idx_1, 66u
next_iteration %16 # -> $B9
}
}
@@ -1395,22 +1395,22 @@
next_iteration %tint_local_index # -> $B5
}
$B5 (%idx:u32): { # body
- %9:bool = gte %idx:u32, 42u
+ %9:bool = gte %idx, 42u
if %9 [t: $B7] { # if_2
$B7: { # true
exit_loop # loop_1
}
}
- %10:ptr<workgroup, i32, read_write> = access %var_c, %idx:u32
+ %10:ptr<workgroup, i32, read_write> = access %var_c, %idx
store %10, 0i
- %11:u32 = mod %idx:u32, 6u
- %12:u32 = div %idx:u32, 6u
+ %11:u32 = mod %idx, 6u
+ %12:u32 = div %idx, 6u
%13:ptr<workgroup, u32, read_write> = access %var_d, %12, %11
store %13, 0u
continue # -> $B6
}
$B6: { # continuing
- %14:u32 = add %idx:u32, 66u
+ %14:u32 = add %idx, 66u
next_iteration %14 # -> $B5
}
}
diff --git a/src/tint/lang/core/ir/validator.cc b/src/tint/lang/core/ir/validator.cc
index d20c427..2299363 100644
--- a/src/tint/lang/core/ir/validator.cc
+++ b/src/tint/lang/core/ir/validator.cc
@@ -42,7 +42,7 @@
#include "src/tint/lang/core/ir/continue.h"
#include "src/tint/lang/core/ir/convert.h"
#include "src/tint/lang/core/ir/core_builtin_call.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/discard.h"
#include "src/tint/lang/core/ir/exit_if.h"
#include "src/tint/lang/core/ir/exit_loop.h"
@@ -336,16 +336,10 @@
const core::type::Type* GetVectorPtrElementType(const Instruction* inst, size_t idx);
private:
- struct Disassembly {
- std::shared_ptr<Source::File> file;
- StyledText text;
- };
-
const Module& mod_;
Capabilities capabilities_;
std::optional<Disassembly> disassembly_;
diag::List diagnostics_;
- Disassembler dis_{mod_};
const Block* current_block_ = nullptr;
Hashset<const Function*, 4> all_functions_;
Hashset<const Instruction*, 4> visited_instructions_;
@@ -360,14 +354,9 @@
Validator::~Validator() = default;
void Validator::DisassembleIfNeeded() {
- if (disassembly_) {
- return;
+ if (!disassembly_) {
+ disassembly_.emplace(Disassemble(mod_));
}
- auto text = dis_.Disassemble();
- disassembly_ = Disassembly{
- std::make_unique<Source::File>("", text.Plain()),
- text,
- };
}
Result<SuccessType> Validator::Run() {
@@ -396,7 +385,7 @@
if (diagnostics_.ContainsErrors()) {
DisassembleIfNeeded();
diagnostics_.AddNote(tint::diag::System::IR, Source{}) << "# Disassembly\n"
- << disassembly_->text;
+ << disassembly_->Text();
return Failure{std::move(diagnostics_)};
}
return Success;
@@ -404,7 +393,7 @@
diag::Diagnostic& Validator::AddError(const Instruction* inst) {
DisassembleIfNeeded();
- auto src = dis_.InstructionSource(inst);
+ auto src = disassembly_->InstructionSource(inst);
auto& diag = AddError(src) << inst->FriendlyName() << ": ";
if (current_block_) {
@@ -415,7 +404,8 @@
diag::Diagnostic& Validator::AddError(const Instruction* inst, size_t idx) {
DisassembleIfNeeded();
- auto src = dis_.OperandSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)});
+ auto src =
+ disassembly_->OperandSource(Disassembly::IndexedValue{inst, static_cast<uint32_t>(idx)});
auto& diag = AddError(src) << inst->FriendlyName() << ": ";
if (current_block_) {
@@ -427,7 +417,8 @@
diag::Diagnostic& Validator::AddResultError(const Instruction* inst, size_t idx) {
DisassembleIfNeeded();
- auto src = dis_.ResultSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)});
+ auto src =
+ disassembly_->ResultSource(Disassembly::IndexedValue{inst, static_cast<uint32_t>(idx)});
auto& diag = AddError(src) << inst->FriendlyName() << ": ";
if (current_block_) {
@@ -438,49 +429,50 @@
diag::Diagnostic& Validator::AddError(const Block* blk) {
DisassembleIfNeeded();
- auto src = dis_.BlockSource(blk);
+ auto src = disassembly_->BlockSource(blk);
return AddError(src);
}
diag::Diagnostic& Validator::AddError(const BlockParam* param) {
DisassembleIfNeeded();
- auto src = dis_.BlockParamSource(param);
+ auto src = disassembly_->BlockParamSource(param);
return AddError(src);
}
diag::Diagnostic& Validator::AddError(const Function* func) {
DisassembleIfNeeded();
- auto src = dis_.FunctionSource(func);
+ auto src = disassembly_->FunctionSource(func);
return AddError(src);
}
diag::Diagnostic& Validator::AddError(const FunctionParam* param) {
DisassembleIfNeeded();
- auto src = dis_.FunctionParamSource(param);
+ auto src = disassembly_->FunctionParamSource(param);
return AddError(src);
}
diag::Diagnostic& Validator::AddNote(const Instruction* inst) {
DisassembleIfNeeded();
- auto src = dis_.InstructionSource(inst);
+ auto src = disassembly_->InstructionSource(inst);
return AddNote(src);
}
diag::Diagnostic& Validator::AddNote(const Function* func) {
DisassembleIfNeeded();
- auto src = dis_.FunctionSource(func);
+ auto src = disassembly_->FunctionSource(func);
return AddNote(src);
}
diag::Diagnostic& Validator::AddNote(const Instruction* inst, size_t idx) {
DisassembleIfNeeded();
- auto src = dis_.OperandSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)});
+ auto src =
+ disassembly_->OperandSource(Disassembly::IndexedValue{inst, static_cast<uint32_t>(idx)});
return AddNote(src);
}
diag::Diagnostic& Validator::AddNote(const Block* blk) {
DisassembleIfNeeded();
- auto src = dis_.BlockSource(blk);
+ auto src = disassembly_->BlockSource(blk);
return AddNote(src);
}
@@ -488,8 +480,8 @@
auto& diag = diagnostics_.AddError(tint::diag::System::IR, src);
if (src.range != Source::Range{{}}) {
DisassembleIfNeeded();
- diag.source.file = disassembly_->file.get();
- diag.owned_file = disassembly_->file;
+ diag.source.file = disassembly_->File().get();
+ diag.owned_file = disassembly_->File();
}
return diag;
}
@@ -498,8 +490,8 @@
auto& diag = diagnostics_.AddNote(tint::diag::System::IR, src);
if (src.range != Source::Range{{}}) {
DisassembleIfNeeded();
- diag.source.file = disassembly_->file.get();
- diag.owned_file = disassembly_->file;
+ diag.source.file = disassembly_->File().get();
+ diag.owned_file = disassembly_->File();
}
return diag;
}
diff --git a/src/tint/lang/core/ir/validator_test.cc b/src/tint/lang/core/ir/validator_test.cc
index 16bb823..6a05c0f 100644
--- a/src/tint/lang/core/ir/validator_test.cc
+++ b/src/tint/lang/core/ir/validator_test.cc
@@ -496,7 +496,7 @@
EXPECT_EQ(res.Failure().reason.Str(),
R"(:4:12 error: destroyed parameter found in block parameter list
$B2 (%my_param:f32): { # body
- ^^^^^^^^^^^^^
+ ^^^^^^^^^
note: # Disassembly
%my_func = func():void {
@@ -530,7 +530,7 @@
EXPECT_EQ(res.Failure().reason.Str(),
R"(:4:12 error: block parameter has nullptr parent block
$B2 (%my_param:f32): { # body
- ^^^^^^^^^^^^^
+ ^^^^^^^^^
note: # Disassembly
%my_func = func():void {
@@ -564,7 +564,7 @@
EXPECT_EQ(res.Failure().reason.Str(),
R"(:4:12 error: block parameter has incorrect parent block
$B2 (%my_param:f32): { # body
- ^^^^^^^^^^^^^
+ ^^^^^^^^^
:7:7 note: parent block declared here
$B3 (%my_param:f32): { # continuing
@@ -575,10 +575,10 @@
$B1: {
loop [b: $B2, c: $B3] { # loop_1
$B2 (%my_param:f32): { # body
- continue %my_param:f32 # -> $B3
+ continue %my_param # -> $B3
}
$B3 (%my_param:f32): { # continuing
- next_iteration %my_param:f32 # -> $B2
+ next_iteration %my_param # -> $B2
}
}
ret
diff --git a/src/tint/lang/spirv/reader/parser/helper_test.h b/src/tint/lang/spirv/reader/parser/helper_test.h
index cf510d3..865aa4c 100644
--- a/src/tint/lang/spirv/reader/parser/helper_test.h
+++ b/src/tint/lang/spirv/reader/parser/helper_test.h
@@ -34,7 +34,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/module.h"
#include "src/tint/lang/core/ir/validator.h"
#include "src/tint/lang/spirv/reader/common/helper_test.h"
diff --git a/src/tint/lang/spirv/reader/reader_test.cc b/src/tint/lang/spirv/reader/reader_test.cc
index cd0626b..d8ab351 100644
--- a/src/tint/lang/spirv/reader/reader_test.cc
+++ b/src/tint/lang/spirv/reader/reader_test.cc
@@ -31,7 +31,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/module.h"
#include "src/tint/lang/core/ir/validator.h"
#include "src/tint/lang/spirv/reader/common/helper_test.h"
diff --git a/src/tint/lang/spirv/writer/raise/var_for_dynamic_index_test.cc b/src/tint/lang/spirv/writer/raise/var_for_dynamic_index_test.cc
index 59b0f27..e1a9da3 100644
--- a/src/tint/lang/spirv/writer/raise/var_for_dynamic_index_test.cc
+++ b/src/tint/lang/spirv/writer/raise/var_for_dynamic_index_test.cc
@@ -587,11 +587,11 @@
$B2 (%5:array<i32, 4>): { # body
if %2 [t: $B3, f: $B4] { # if_1
$B3: { # true
- %6:i32 = access %5:array<i32, 4>, %3
+ %6:i32 = access %5, %3
ret %6
}
$B4: { # false
- %7:i32 = access %5:array<i32, 4>, %4
+ %7:i32 = access %5, %4
ret %7
}
}
@@ -609,7 +609,7 @@
$B1: {
loop [b: $B2] { # loop_1
$B2 (%5:array<i32, 4>): { # body
- %6:ptr<function, array<i32, 4>, read_write> = var, %5:array<i32, 4>
+ %6:ptr<function, array<i32, 4>, read_write> = var, %5
if %2 [t: $B3, f: $B4] { # if_1
$B3: { # true
%7:ptr<function, i32, read_write> = access %6, %3
@@ -666,11 +666,11 @@
$B2 (%5:array<array<i32, 4>, 4>): { # body
if %2 [t: $B3, f: $B4] { # if_1
$B3: { # true
- %6:i32 = access %5:array<array<i32, 4>, 4>, 0u, %3
+ %6:i32 = access %5, 0u, %3
ret %6
}
$B4: { # false
- %7:i32 = access %5:array<array<i32, 4>, 4>, 0u, %4
+ %7:i32 = access %5, 0u, %4
ret %7
}
}
@@ -688,7 +688,7 @@
$B1: {
loop [b: $B2] { # loop_1
$B2 (%5:array<array<i32, 4>, 4>): { # body
- %6:array<i32, 4> = access %5:array<array<i32, 4>, 4>, 0u
+ %6:array<i32, 4> = access %5, 0u
%7:ptr<function, array<i32, 4>, read_write> = var, %6
if %2 [t: $B3, f: $B4] { # if_1
$B3: { # true
diff --git a/src/tint/lang/wgsl/helpers/ir_program_test.h b/src/tint/lang/wgsl/helpers/ir_program_test.h
index 6a71008..5468ebc 100644
--- a/src/tint/lang/wgsl/helpers/ir_program_test.h
+++ b/src/tint/lang/wgsl/helpers/ir_program_test.h
@@ -35,7 +35,7 @@
#include <utility>
#include "gtest/gtest.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/validator.h"
#include "src/tint/lang/core/number.h"
#include "src/tint/lang/wgsl/program/program_builder.h"
diff --git a/src/tint/lang/wgsl/ir_roundtrip_fuzz.cc b/src/tint/lang/wgsl/ir_roundtrip_fuzz.cc
index 0265906..47af44ac 100644
--- a/src/tint/lang/wgsl/ir_roundtrip_fuzz.cc
+++ b/src/tint/lang/wgsl/ir_roundtrip_fuzz.cc
@@ -30,7 +30,7 @@
#include <iostream>
#include "src/tint/cmd/fuzz/ir/fuzz.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/wgsl/reader/lower/lower.h"
#include "src/tint/lang/wgsl/reader/parser/parser.h"
#include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h"
diff --git a/src/tint/lang/wgsl/ir_roundtrip_test.cc b/src/tint/lang/wgsl/ir_roundtrip_test.cc
index ac07621..39c7ca9 100644
--- a/src/tint/lang/wgsl/ir_roundtrip_test.cc
+++ b/src/tint/lang/wgsl/ir_roundtrip_test.cc
@@ -29,7 +29,7 @@
#include "gtest/gtest.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h"
#include "src/tint/lang/wgsl/reader/reader.h"
#include "src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.h"
diff --git a/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc b/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc
index 68d4db1..5e601cc 100644
--- a/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc
+++ b/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc
@@ -32,7 +32,7 @@
#include "src/tint/lang/core/access.h"
#include "src/tint/lang/core/address_space.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/texel_format.h"
#include "src/tint/lang/core/type/storage_texture.h"
#include "src/tint/lang/core/type/texture_dimension.h"
diff --git a/src/tint/lang/wgsl/writer/raise/ptr_to_ref_test.cc b/src/tint/lang/wgsl/writer/raise/ptr_to_ref_test.cc
index 3862749..40f02f4 100644
--- a/src/tint/lang/wgsl/writer/raise/ptr_to_ref_test.cc
+++ b/src/tint/lang/wgsl/writer/raise/ptr_to_ref_test.cc
@@ -32,7 +32,7 @@
#include "gtest/gtest.h"
#include "src/tint/lang/core/ir/builder.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/validator.h"
namespace tint::wgsl::writer::raise {
@@ -65,10 +65,7 @@
}
/// @returns the transformed module as a disassembled string
- std::string str() {
- core::ir::Disassembler dis(mod);
- return "\n" + dis.Disassemble().Plain();
- }
+ std::string str() { return "\n" + core::ir::Disassemble(mod).Plain(); }
protected:
/// The test IR module.
diff --git a/src/tint/lang/wgsl/writer/raise/rename_conflicts_test.cc b/src/tint/lang/wgsl/writer/raise/rename_conflicts_test.cc
index 7be25fc..d5a25db 100644
--- a/src/tint/lang/wgsl/writer/raise/rename_conflicts_test.cc
+++ b/src/tint/lang/wgsl/writer/raise/rename_conflicts_test.cc
@@ -32,7 +32,7 @@
#include "gtest/gtest.h"
#include "src/tint/lang/core/ir/builder.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/validator.h"
#include "src/tint/lang/core/type/matrix.h"
@@ -65,10 +65,7 @@
}
/// @returns the transformed module as a disassembled string
- std::string str() {
- core::ir::Disassembler dis(mod);
- return "\n" + dis.Disassemble().Plain();
- }
+ std::string str() { return "\n" + core::ir::Disassemble(mod).Plain(); }
protected:
/// The test IR module.
diff --git a/src/tint/lang/wgsl/writer/writer_test.cc b/src/tint/lang/wgsl/writer/writer_test.cc
index ca2f339..d9e573c 100644
--- a/src/tint/lang/wgsl/writer/writer_test.cc
+++ b/src/tint/lang/wgsl/writer/writer_test.cc
@@ -32,7 +32,7 @@
#include <string_view>
#include "gtest/gtest.h"
-#include "src/tint/lang/core/ir/disassembler.h"
+#include "src/tint/lang/core/ir/disassembly.h"
#include "src/tint/lang/core/ir/ir_helper_test.h"
#include "src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.h"
#include "src/tint/lang/wgsl/writer/ir_to_program/program_options.h"