-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tint] Update insertion point in InsertAfter()
When inserting multiple instructions using `InsertAfter()`, it's reasonable to expect that the instructions created inside the lambda retain the order that they are created in. This change makes that the case by updating the insertion point after inserting each instruction. Add some basic tests for the insertion helpers. Change-Id: I0eb850675dd9a136818a6b4cadbb63126363ab54 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/203316 Reviewed-by: dan sinclair <[email protected]> Commit-Queue: James Price <[email protected]>
- Loading branch information
Showing
6 changed files
with
272 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
// 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/core/ir/builder.h" | ||
|
||
#include "gmock/gmock.h" | ||
#include "src/tint/lang/core/ir/ir_helper_test.h" | ||
|
||
namespace tint::core::ir { | ||
namespace { | ||
|
||
using namespace tint::core::number_suffixes; // NOLINT | ||
using IR_BuilderTest = IRTestHelper; | ||
|
||
TEST_F(IR_BuilderTest, Append) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
b.Append(func->Block(), [&] { | ||
b.Let("a", 1_u); | ||
b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
b.Append(func->Block(), [&] { | ||
b.Let("d", 4_u); | ||
b.Let("e", 5_u); | ||
b.Let("f", 6_u); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func():void { | ||
$B1: { | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%c:u32 = let 3u | ||
%d:u32 = let 4u | ||
%e:u32 = let 5u | ||
%f:u32 = let 6u | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertBefore) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
Instruction* ip = nullptr; | ||
b.Append(func->Block(), [&] { | ||
b.Let("a", 1_u); | ||
ip = b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
b.InsertBefore(ip, [&] { | ||
b.Let("d", 4_u); | ||
b.Let("e", 5_u); | ||
b.Let("f", 6_u); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func():void { | ||
$B1: { | ||
%a:u32 = let 1u | ||
%d:u32 = let 4u | ||
%e:u32 = let 5u | ||
%f:u32 = let 6u | ||
%b:u32 = let 2u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertAfter) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
Instruction* ip = nullptr; | ||
b.Append(func->Block(), [&] { | ||
b.Let("a", 1_u); | ||
ip = b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
b.InsertAfter(ip, [&] { | ||
b.Let("d", 4_u); | ||
b.Let("e", 5_u); | ||
b.Let("f", 6_u); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func():void { | ||
$B1: { | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%d:u32 = let 4u | ||
%e:u32 = let 5u | ||
%f:u32 = let 6u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertAfter_InstructionResult) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
Instruction* ip = nullptr; | ||
b.Append(func->Block(), [&] { | ||
b.Let("a", 1_u); | ||
ip = b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
b.InsertAfter(ip->Result(0), [&] { | ||
b.Let("d", 4_u); | ||
b.Let("e", 5_u); | ||
b.Let("f", 6_u); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func():void { | ||
$B1: { | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%d:u32 = let 4u | ||
%e:u32 = let 5u | ||
%f:u32 = let 6u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertAfter_Param) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
auto* param = b.FunctionParam("param", ty.u32()); | ||
func->SetParams({param}); | ||
b.Append(func->Block(), [&] { | ||
b.Let("a", 1_u); | ||
b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
b.InsertAfter(param, [&] { | ||
b.Let("d", 4_u); | ||
b.Let("e", 5_u); | ||
b.Let("f", 6_u); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func(%param:u32):void { | ||
$B1: { | ||
%d:u32 = let 4u | ||
%e:u32 = let 5u | ||
%f:u32 = let 6u | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertAfter_Param_EmptyFunction) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
auto* param = b.FunctionParam("param", ty.u32()); | ||
func->SetParams({param}); | ||
b.InsertAfter(param, [&] { | ||
b.Let("a", 1_u); | ||
b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func(%param:u32):void { | ||
$B1: { | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertAfter_BlockParam) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
b.Append(func->Block(), [&] { | ||
auto* loop = b.Loop(); | ||
auto* param = b.BlockParam("param", ty.u32()); | ||
loop->Body()->SetParams({param}); | ||
b.Append(loop->Body(), [&] { | ||
b.Let("a", 1_u); | ||
b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
b.InsertAfter(param, [&] { | ||
b.Let("d", 4_u); | ||
b.Let("e", 5_u); | ||
b.Let("f", 6_u); | ||
}); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func():void { | ||
$B1: { | ||
loop [b: $B2] { # loop_1 | ||
$B2 (%param:u32): { # body | ||
%d:u32 = let 4u | ||
%e:u32 = let 5u | ||
%f:u32 = let 6u | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
TEST_F(IR_BuilderTest, InsertAfter_BlockParam_EmptyBlock) { | ||
auto* func = b.Function("foo", ty.void_()); | ||
b.Append(func->Block(), [&] { | ||
auto* loop = b.Loop(); | ||
auto* param = b.BlockParam("param", ty.u32()); | ||
loop->Body()->SetParams({param}); | ||
b.InsertAfter(param, [&] { | ||
b.Let("a", 1_u); | ||
b.Let("b", 2_u); | ||
b.Let("c", 3_u); | ||
}); | ||
}); | ||
EXPECT_EQ(str(), R"( | ||
%foo = func():void { | ||
$B1: { | ||
loop [b: $B2] { # loop_1 | ||
$B2 (%param:u32): { # body | ||
%a:u32 = let 1u | ||
%b:u32 = let 2u | ||
%c:u32 = let 3u | ||
} | ||
} | ||
} | ||
} | ||
)"); | ||
} | ||
|
||
} // namespace | ||
} // namespace tint::core::ir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters