Skip to content

Commit 5021631

Browse files
committed
[tint][ir] Make type construction and conversion unsequenced
This allows them to be inlined in more places. Change-Id: Id42e11bd48d862d210180ed1dc68403cc19711ec Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/153120 Reviewed-by: James Price <[email protected]> Kokoro: Kokoro <[email protected]>
1 parent dfc815c commit 5021631

File tree

9 files changed

+432
-16
lines changed

9 files changed

+432
-16
lines changed

src/tint/lang/core/ir/builder.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,15 @@ class Builder {
654654
/// @returns the instruction
655655
ir::Discard* Discard();
656656

657+
/// Creates a user function call instruction
658+
/// @param func the function to call
659+
/// @param args the call arguments
660+
/// @returns the instruction
661+
template <typename... ARGS>
662+
ir::UserCall* Call(ir::Function* func, ARGS&&... args) {
663+
return Call(func->ReturnType(), func, std::forward<ARGS>(args)...);
664+
}
665+
657666
/// Creates a user function call instruction
658667
/// @param type the return type of the call
659668
/// @param func the function to call
@@ -688,6 +697,15 @@ class Builder {
688697
Values(std::forward<ARGS>(args)...)));
689698
}
690699

700+
/// Creates a value conversion instruction to the template type T
701+
/// @param val the value to be converted
702+
/// @returns the instruction
703+
template <typename T, typename VAL>
704+
ir::Convert* Convert(VAL&& val) {
705+
auto* type = ir.Types().Get<T>();
706+
return Convert(type, std::forward<VAL>(val));
707+
}
708+
691709
/// Creates a value conversion instruction
692710
/// @param to the type converted to
693711
/// @param val the value to be converted
@@ -698,6 +716,15 @@ class Builder {
698716
Value(std::forward<VAL>(val))));
699717
}
700718

719+
/// Creates a value constructor instruction to the template type T
720+
/// @param args the arguments to the constructor
721+
/// @returns the instruction
722+
template <typename T, typename... ARGS>
723+
ir::Construct* Construct(ARGS&&... args) {
724+
auto* type = ir.Types().Get<T>();
725+
return Construct(type, std::forward<ARGS>(args)...);
726+
}
727+
701728
/// Creates a value constructor instruction
702729
/// @param type the type to constructed
703730
/// @param args the arguments to the constructor
@@ -768,6 +795,25 @@ class Builder {
768795
/// @returns the instruction
769796
ir::Var* Var(std::string_view name, const core::type::Pointer* type);
770797

798+
/// Creates a new `var` declaration with a name and initializer value
799+
/// @param name the var name
800+
/// @param init the var initializer
801+
/// @returns the instruction
802+
template <core::AddressSpace SPACE = core::AddressSpace::kFunction,
803+
core::Access ACCESS = core::Access::kReadWrite,
804+
typename VALUE = void>
805+
ir::Var* Var(std::string_view name, VALUE&& init) {
806+
auto* val = Value(std::forward<VALUE>(init));
807+
if (TINT_UNLIKELY(!val)) {
808+
TINT_ASSERT(val);
809+
return nullptr;
810+
}
811+
auto* var = Var(name, ir.Types().ptr(SPACE, val->Type(), ACCESS));
812+
var->SetInitializer(val);
813+
ir.SetName(var->Result(), name);
814+
return var;
815+
}
816+
771817
/// Creates a new `let` declaration
772818
/// @param name the let name
773819
/// @param value the let value

src/tint/lang/core/ir/builtin_call.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::core::ir::BuiltinCall);
2121
namespace tint::core::ir {
2222

2323
BuiltinCall::BuiltinCall(InstructionResult* result, VectorRef<Value*> arguments) {
24+
flags_.Add(Flag::kSequenced);
2425
AddOperands(BuiltinCall::kArgsOperandOffset, std::move(arguments));
2526
AddResult(result);
2627
}

src/tint/lang/core/ir/call.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::core::ir::Call);
2020

2121
namespace tint::core::ir {
2222

23-
Call::Call() {
24-
flags_.Add(Flag::kSequenced);
25-
}
23+
Call::Call() = default;
2624

2725
Call::~Call() = default;
2826

src/tint/lang/core/ir/core_builtin_call_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TEST_F(IR_CoreBuiltinCallTest, Fail_NullType) {
4848
{
4949
Module mod;
5050
Builder b{mod};
51-
b.Call(nullptr, core::BuiltinFn::kAbs);
51+
b.Call(static_cast<type::Type*>(nullptr), core::BuiltinFn::kAbs);
5252
},
5353
"");
5454
}

src/tint/lang/core/ir/discard.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ TINT_INSTANTIATE_TYPEINFO(tint::core::ir::Discard);
2121

2222
namespace tint::core::ir {
2323

24-
Discard::Discard() = default;
24+
Discard::Discard() {
25+
flags_.Add(Flag::kSequenced);
26+
}
2527

2628
Discard::~Discard() = default;
2729

src/tint/lang/core/ir/user_call.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::core::ir::UserCall);
2424
namespace tint::core::ir {
2525

2626
UserCall::UserCall(InstructionResult* result, Function* func, VectorRef<Value*> arguments) {
27+
flags_.Add(Flag::kSequenced);
2728
AddOperand(UserCall::kFunctionOperandOffset, func);
2829
AddOperands(UserCall::kArgsOperandOffset, std::move(arguments));
2930
AddResult(result);

src/tint/lang/core/ir/user_call_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ TEST_F(IR_UserCallTest, Fail_NullType) {
5151
{
5252
Module mod;
5353
Builder b{mod};
54-
b.Call(nullptr, b.Function("myfunc", mod.Types().void_()));
54+
b.Call(static_cast<type::Type*>(nullptr), b.Function("myfunc", mod.Types().void_()));
5555
},
5656
"");
5757
}

src/tint/lang/wgsl/writer/ir_to_program/inlining_test.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,8 @@ fn a(v : i32) -> i32 {
735735
}
736736
737737
fn f() -> f32 {
738-
let v_1 = array<mat3x4<f32>, 5u>();
739-
let v_2 = a(2i);
740-
return v_1[a(1i)][v_2][a(3i)];
738+
let v_1 = a(2i);
739+
return array<mat3x4<f32>, 5u>()[a(1i)][v_1][a(3i)];
741740
}
742741
)");
743742
}
@@ -763,9 +762,8 @@ fn a(v : i32) -> i32 {
763762
}
764763
765764
fn f() -> f32 {
766-
let v_1 = array<mat3x4<f32>, 5u>();
767-
let v_2 = a(3i);
768-
return v_1[a(1i)][a(2i)][v_2];
765+
let v_1 = a(3i);
766+
return array<mat3x4<f32>, 5u>()[a(1i)][a(2i)][v_1];
769767
}
770768
)");
771769
}
@@ -791,10 +789,9 @@ fn a(v : i32) -> i32 {
791789
}
792790
793791
fn f() -> f32 {
794-
let v_1 = array<mat3x4<f32>, 5u>();
795-
let v_2 = a(3i);
796-
let v_3 = a(2i);
797-
return v_1[a(1i)][v_3][v_2];
792+
let v_1 = a(3i);
793+
let v_2 = a(2i);
794+
return array<mat3x4<f32>, 5u>()[a(1i)][v_2][v_1];
798795
}
799796
)");
800797
}

0 commit comments

Comments
 (0)