[ir] Move `ir::Bitcast` to inherit `ir::Call`.

This CL moves `ir::Bitcast` from `ir::Instruction` to `ir::Call`. The
`bitcast` is, essentially, a templated call instruction.

Bug: tint:1904
Change-Id: Iab8ccffaa484767318433f5fc6c33670c5f6375e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/129940
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2023-04-27 20:28:01 +00:00 committed by Dawn LUCI CQ
parent 4a2e0ad36b
commit f26b1269bd
3 changed files with 12 additions and 18 deletions

View File

@ -19,16 +19,14 @@ TINT_INSTANTIATE_TYPEINFO(tint::ir::Bitcast);
namespace tint::ir {
Bitcast::Bitcast(uint32_t id, const type::Type* type, Value* val) : Base(id, type), val_(val) {
TINT_ASSERT(IR, val_);
val_->AddUsage(this);
}
Bitcast::Bitcast(uint32_t id, const type::Type* type, Value* val)
: Base(id, type, utils::Vector{val}) {}
Bitcast::~Bitcast() = default;
utils::StringStream& Bitcast::ToInstruction(utils::StringStream& out) const {
ToValue(out) << " = bitcast(";
val_->ToValue(out);
EmitArgs(out);
out << ")";
return out;
}

View File

@ -15,14 +15,14 @@
#ifndef SRC_TINT_IR_BITCAST_H_
#define SRC_TINT_IR_BITCAST_H_
#include "src/tint/ir/instruction.h"
#include "src/tint/ir/call.h"
#include "src/tint/utils/castable.h"
#include "src/tint/utils/string_stream.h"
namespace tint::ir {
/// A bitcast instruction in the IR.
class Bitcast : public utils::Castable<Bitcast, Instruction> {
class Bitcast : public utils::Castable<Bitcast, Call> {
public:
/// Constructor
/// @param id the instruction id
@ -36,16 +36,10 @@ class Bitcast : public utils::Castable<Bitcast, Instruction> {
Bitcast& operator=(const Bitcast& inst) = delete;
Bitcast& operator=(Bitcast&& inst) = delete;
/// @returns the left-hand-side value for the instruction
const Value* Val() const { return val_; }
/// Write the instruction to the given stream
/// @param out the stream to write to
/// @returns the stream
utils::StringStream& ToInstruction(utils::StringStream& out) const override;
private:
Value* val_ = nullptr;
};
} // namespace tint::ir

View File

@ -31,8 +31,9 @@ TEST_F(IR_InstructionTest, Bitcast) {
ASSERT_TRUE(inst->Is<ir::Bitcast>());
ASSERT_NE(inst->Type(), nullptr);
ASSERT_TRUE(inst->Val()->Is<Constant>());
auto val = inst->Val()->As<Constant>()->value;
ASSERT_EQ(inst->Args().Length(), 1u);
ASSERT_TRUE(inst->Args()[0]->Is<Constant>());
auto val = inst->Args()[0]->As<Constant>()->value;
ASSERT_TRUE(val->Is<constant::Scalar<i32>>());
EXPECT_EQ(4_i, val->As<constant::Scalar<i32>>()->ValueAs<i32>());
@ -46,9 +47,10 @@ TEST_F(IR_InstructionTest, Bitcast_Usage) {
const auto* inst =
b.builder.Bitcast(b.builder.ir.types.Get<type::I32>(), b.builder.Constant(4_i));
ASSERT_NE(inst->Val(), nullptr);
ASSERT_EQ(inst->Val()->Usage().Length(), 1u);
EXPECT_EQ(inst->Val()->Usage()[0], inst);
ASSERT_EQ(inst->Args().Length(), 1u);
ASSERT_NE(inst->Args()[0], nullptr);
ASSERT_EQ(inst->Args()[0]->Usage().Length(), 1u);
EXPECT_EQ(inst->Args()[0]->Usage()[0], inst);
}
} // namespace