Skip to content

Commit

Permalink
[glsl][ir] Add conversion support
Browse files Browse the repository at this point in the history
This Cl adds `Convert` support to the GLSL IR backend.

Bug: 42251044
Change-Id: I43c7e470b4d055fecebcff0a3db8edb7643ca314
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/204894
Reviewed-by: James Price <[email protected]>
Commit-Queue: dan sinclair <[email protected]>
  • Loading branch information
dj2 authored and Dawn LUCI CQ committed Sep 3, 2024
1 parent 7abbf53 commit fb96429
Show file tree
Hide file tree
Showing 181 changed files with 1,949 additions and 1,755 deletions.
1 change: 1 addition & 0 deletions src/tint/lang/glsl/writer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ cc_library(
"call_test.cc",
"constant_test.cc",
"constructor_test.cc",
"convert_test.cc",
"function_test.cc",
"if_test.cc",
"loop_test.cc",
Expand Down
1 change: 1 addition & 0 deletions src/tint/lang/glsl/writer/BUILD.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ tint_add_target(tint_lang_glsl_writer_test test
lang/glsl/writer/call_test.cc
lang/glsl/writer/constant_test.cc
lang/glsl/writer/constructor_test.cc
lang/glsl/writer/convert_test.cc
lang/glsl/writer/function_test.cc
lang/glsl/writer/if_test.cc
lang/glsl/writer/loop_test.cc
Expand Down
1 change: 1 addition & 0 deletions src/tint/lang/glsl/writer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ if (tint_build_unittests) {
"call_test.cc",
"constant_test.cc",
"constructor_test.cc",
"convert_test.cc",
"function_test.cc",
"if_test.cc",
"loop_test.cc",
Expand Down
56 changes: 56 additions & 0 deletions src/tint/lang/glsl/writer/convert_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// 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/glsl/writer/helper_test.h"

using namespace tint::core::fluent_types; // NOLINT
using namespace tint::core::number_suffixes; // NOLINT

namespace tint::glsl::writer {
namespace {

TEST_F(GlslWriterTest, ConvertU32) {
auto* f = b.Function("a", ty.u32());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_i);
b.Return(f, b.Convert(ty.u32(), v));
});

ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uint a() {
int v = 2;
return uint(v);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
)");
}

} // namespace
} // namespace tint::glsl::writer
10 changes: 10 additions & 0 deletions src/tint/lang/glsl/writer/printer/printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#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_binary.h"
#include "src/tint/lang/core/ir/core_builtin_call.h"
#include "src/tint/lang/core/ir/core_unary.h"
Expand Down Expand Up @@ -802,6 +803,7 @@ class Printer : public tint::TextGenerator {
r->Instruction(), //
[&](const core::ir::Access* a) { EmitAccess(out, a); },
[&](const core::ir::Construct* c) { EmitConstruct(out, c); },
[&](const core::ir::Convert* c) { EmitConvert(out, c); }, //
[&](const core::ir::CoreBinary* b) { EmitBinary(out, b); },
[&](const core::ir::CoreBuiltinCall* c) { EmitCoreBuiltinCall(out, c); },
[&](const core::ir::CoreUnary* u) { EmitUnary(out, u); },
Expand All @@ -820,6 +822,14 @@ class Printer : public tint::TextGenerator {
TINT_ICE_ON_NO_MATCH);
}

/// Emit a convert instruction
void EmitConvert(StringStream& out, const core::ir::Convert* c) {
EmitType(out, c->Result(0)->Type());
out << "(";
EmitValue(out, c->Operand(0));
out << ")";
}

/// Emit a constructor
void EmitConstruct(StringStream& out, const core::ir::Construct* c) {
if (c->Args().IsEmpty()) {
Expand Down
21 changes: 11 additions & 10 deletions test/tint/bug/tint/1563.wgsl.expected.ir.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
SKIP: FAILED
#version 310 es

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Access
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float foo() {
int oob = 99;
float b = vec4(0.0f)[min(uint(oob), 3u)];
vec4 v = vec4(0.0f);
v[min(uint(oob), 3u)] = b;
return b;
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:252 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Store
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float16_t t = 0.0hf;
f16mat2 m() {
t = (t + 1.0hf);
return f16mat2(f16vec2(1.0hf, 2.0hf), f16vec2(3.0hf, 4.0hf));
}
void f() {
mat2 v = mat2(m());
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:252 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Store
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float t = 0.0f;
mat2 m() {
t = (t + 1.0f);
return mat2(vec2(1.0f, 2.0f), vec2(3.0f, 4.0f));
}
void f() {
f16mat2 v = f16mat2(m());
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Convert
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
f16mat2 u = f16mat2(f16vec2(1.0hf, 2.0hf), f16vec2(3.0hf, 4.0hf));
void f() {
mat2 v = mat2(u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Convert
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
mat2 u = mat2(vec2(1.0f, 2.0f), vec2(3.0f, 4.0f));
void f() {
f16mat2 v = f16mat2(u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:252 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Store
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float16_t t = 0.0hf;
f16mat2x3 m() {
t = (t + 1.0hf);
return f16mat2x3(f16vec3(1.0hf, 2.0hf, 3.0hf), f16vec3(4.0hf, 5.0hf, 6.0hf));
}
void f() {
mat2x3 v = mat2x3(m());
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:252 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Store
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float t = 0.0f;
mat2x3 m() {
t = (t + 1.0f);
return mat2x3(vec3(1.0f, 2.0f, 3.0f), vec3(4.0f, 5.0f, 6.0f));
}
void f() {
f16mat2x3 v = f16mat2x3(m());
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Convert
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
f16mat2x3 u = f16mat2x3(f16vec3(1.0hf, 2.0hf, 3.0hf), f16vec3(4.0hf, 5.0hf, 6.0hf));
void f() {
mat2x3 v = mat2x3(u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Convert
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
mat2x3 u = mat2x3(vec3(1.0f, 2.0f, 3.0f), vec3(4.0f, 5.0f, 6.0f));
void f() {
f16mat2x3 v = f16mat2x3(u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:252 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Store
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float16_t t = 0.0hf;
f16mat2x4 m() {
t = (t + 1.0hf);
return f16mat2x4(f16vec4(1.0hf, 2.0hf, 3.0hf, 4.0hf), f16vec4(5.0hf, 6.0hf, 7.0hf, 8.0hf));
}
void f() {
mat2x4 v = mat2x4(m());
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:252 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Store
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
float t = 0.0f;
mat2x4 m() {
t = (t + 1.0f);
return mat2x4(vec4(1.0f, 2.0f, 3.0f, 4.0f), vec4(5.0f, 6.0f, 7.0f, 8.0f));
}
void f() {
f16mat2x4 v = f16mat2x4(m());
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
SKIP: FAILED
#version 310 es
#extension GL_AMD_gpu_shader_half_float: require

<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::Convert
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************

tint executable returned error: signal: illegal instruction
f16mat2x4 u = f16mat2x4(f16vec4(1.0hf, 2.0hf, 3.0hf, 4.0hf), f16vec4(5.0hf, 6.0hf, 7.0hf, 8.0hf));
void f() {
mat2x4 v = mat2x4(u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
Loading

0 comments on commit fb96429

Please sign in to comment.