Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/NZSL/Ast/Transformations/ForToWhileTransformer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ namespace nzsl::Ast

private:
using Transformer::Transform;
StatementTransformation Transform(ContinueStatement&& statement) override;
StatementTransformation Transform(ForEachStatement&& statement) override;
StatementTransformation Transform(ForStatement&& statement) override;

ExpressionPtr* m_incrExpr;
const Options* m_options;
};
}
Expand Down
37 changes: 27 additions & 10 deletions src/NZSL/Ast/Transformations/ForToWhileTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <NZSL/Ast/Transformations/ForToWhileTransformer.hpp>
#include <NZSL/Ast/Cloner.hpp>
#include <NZSL/Ast/Utils.hpp>
#include <NZSL/Lang/Errors.hpp>
#include <NZSL/Ast/Transformations/TransformerContext.hpp>
Expand All @@ -19,6 +20,19 @@ namespace nzsl::Ast
return TransformModule(module, context, error);
}

auto ForToWhileTransformer::Transform(ContinueStatement&& statement) -> StatementTransformation
{
if (!m_incrExpr)
return DontVisitChildren{};

auto multi = std::make_unique<MultiStatement>();
multi->sourceLocation = statement.sourceLocation;
multi->statements.reserve(2);
multi->statements.emplace_back(ShaderBuilder::ExpressionStatement(Clone(*(*m_incrExpr))));
multi->statements.emplace_back(std::move(GetCurrentStatementPtr()));
return ReplaceStatement{ std::move(multi) };
}

auto ForToWhileTransformer::Transform(ForEachStatement&& forEachStatement) -> StatementTransformation
{
if (!m_options->reduceForEachLoopsToWhile)
Expand All @@ -31,14 +45,11 @@ namespace nzsl::Ast
if (!IsArrayType(*exprType))
throw CompilerForEachUnsupportedTypeError{ forEachStatement.sourceLocation, ToString(*exprType, forEachStatement.sourceLocation) };

HandleStatement(forEachStatement.statement);

const ArrayType& arrayType = std::get<ArrayType>(*exprType);
const ExpressionType& innerType = ResolveAlias(arrayType.InnerType());

auto multi = std::make_unique<MultiStatement>();
multi->sourceLocation = forEachStatement.sourceLocation;

multi->statements.reserve(2);

// Counter variable
Expand Down Expand Up @@ -72,13 +83,17 @@ namespace nzsl::Ast
elementVariable->sourceLocation = forEachStatement.sourceLocation;
elementVariable->varIndex = forEachStatement.varIndex; //< Preserve var index

body->statements.emplace_back(std::move(elementVariable));
body->statements.emplace_back(Unscope(std::move(forEachStatement.statement)));

auto incrCounter = ShaderBuilder::Assign(AssignType::CompoundAdd, ShaderBuilder::Variable(counterVarIndex, PrimitiveType::UInt32, forEachStatement.sourceLocation), ShaderBuilder::ConstantValue(1u, forEachStatement.sourceLocation));
ExpressionPtr incrCounter = ShaderBuilder::Assign(AssignType::CompoundAdd, ShaderBuilder::Variable(counterVarIndex, PrimitiveType::UInt32, forEachStatement.sourceLocation), ShaderBuilder::ConstantValue(1u, forEachStatement.sourceLocation));
incrCounter->cachedExpressionType = PrimitiveType::UInt32;
incrCounter->sourceLocation = forEachStatement.sourceLocation;

m_incrExpr = &incrCounter;
HandleStatement(forEachStatement.statement);
m_incrExpr = nullptr;

body->statements.emplace_back(std::move(elementVariable));
body->statements.emplace_back(Unscope(std::move(forEachStatement.statement)));

body->statements.emplace_back(ShaderBuilder::ExpressionStatement(std::move(incrCounter)));

whileStatement->body = std::move(body);
Expand Down Expand Up @@ -113,8 +128,6 @@ namespace nzsl::Ast
return ExpressionType{ counterType };
};

HandleStatement(forStatement.statement);

auto multi = std::make_unique<MultiStatement>();
multi->sourceLocation = forStatement.sourceLocation;

Expand Down Expand Up @@ -175,10 +188,14 @@ namespace nzsl::Ast

incrExpr->sourceLocation = forStatement.sourceLocation;

auto incrCounter = ShaderBuilder::Assign(AssignType::CompoundAdd, ShaderBuilder::Variable(counterVarIndex, counterType, forStatement.sourceLocation), std::move(incrExpr));
ExpressionPtr incrCounter = ShaderBuilder::Assign(AssignType::CompoundAdd, ShaderBuilder::Variable(counterVarIndex, counterType, forStatement.sourceLocation), std::move(incrExpr));
incrCounter->cachedExpressionType = PrimitiveType::UInt32;
incrCounter->sourceLocation = forStatement.sourceLocation;

m_incrExpr = &incrCounter;
HandleStatement(forStatement.statement);
m_incrExpr = nullptr;

body->statements.emplace_back(Unscope(std::move(forStatement.statement)));
body->statements.emplace_back(ShaderBuilder::ExpressionStatement(std::move(incrCounter)));

Expand Down
64 changes: 36 additions & 28 deletions tests/src/Tests/LoopTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ void main()
{
if (v == 4)
{
v += 1;
continue;
}

Expand Down Expand Up @@ -508,26 +509,29 @@ fn main()
OpSelectionMerge %23 SelectionControl(0)
OpBranchConditional %27 %24 %25
%24 = OpLabel
%28 = OpLoad %3 %14
%29 = OpIAdd %3 %28 %9
OpStore %14 %29
OpBranch %19
%25 = OpLabel
OpBranch %23
%23 = OpLabel
%28 = OpLoad %3 %13
%29 = OpLoad %3 %14
%30 = OpIAdd %3 %28 %29
OpStore %13 %30
%34 = OpLoad %3 %14
%35 = OpSGreaterThanEqual %7 %34 %9
OpSelectionMerge %31 SelectionControl(0)
OpBranchConditional %35 %32 %33
%32 = OpLabel
%30 = OpLoad %3 %13
%31 = OpLoad %3 %14
%32 = OpIAdd %3 %30 %31
OpStore %13 %32
%36 = OpLoad %3 %14
%37 = OpSGreaterThanEqual %7 %36 %10
OpSelectionMerge %33 SelectionControl(0)
OpBranchConditional %37 %34 %35
%34 = OpLabel
OpBranch %18
%35 = OpLabel
OpBranch %33
%33 = OpLabel
OpBranch %31
%31 = OpLabel
%36 = OpLoad %3 %14
%37 = OpIAdd %3 %36 %10
OpStore %14 %37
%38 = OpLoad %3 %14
%39 = OpIAdd %3 %38 %9
OpStore %14 %39
OpBranch %19
%19 = OpLabel
OpBranch %16
Expand Down Expand Up @@ -766,6 +770,7 @@ void main()
float v = data.value[_nzsl_counter];
if (v < 0.0)
{
_nzsl_counter += 1u;
continue;
}

Expand Down Expand Up @@ -830,26 +835,29 @@ fn main()
OpSelectionMerge %35 SelectionControl(0)
OpBranchConditional %39 %36 %37
%36 = OpLabel
%40 = OpLoad %2 %23
%41 = OpIAdd %2 %40 %18
OpStore %23 %41
OpBranch %28
%37 = OpLabel
OpBranch %35
%35 = OpLabel
%40 = OpLoad %1 %22
%41 = OpLoad %1 %24
%42 = OpFAdd %1 %40 %41
OpStore %22 %42
%46 = OpLoad %1 %22
%47 = OpFOrdGreaterThanEqual %14 %46 %18
OpSelectionMerge %43 SelectionControl(0)
OpBranchConditional %47 %44 %45
%44 = OpLabel
%42 = OpLoad %1 %22
%43 = OpLoad %1 %24
%44 = OpFAdd %1 %42 %43
OpStore %22 %44
%48 = OpLoad %1 %22
%49 = OpFOrdGreaterThanEqual %14 %48 %19
OpSelectionMerge %45 SelectionControl(0)
OpBranchConditional %49 %46 %47
%46 = OpLabel
OpBranch %27
%47 = OpLabel
OpBranch %45
%45 = OpLabel
OpBranch %43
%43 = OpLabel
%48 = OpLoad %2 %23
%49 = OpIAdd %2 %48 %19
OpStore %23 %49
%50 = OpLoad %2 %23
%51 = OpIAdd %2 %50 %18
OpStore %23 %51
OpBranch %28
%28 = OpLabel
OpBranch %25
Expand Down
Loading