Skip to content

Commit

Permalink
[tint] Add a const param validator
Browse files Browse the repository at this point in the history
Override parameters need to be const evaluated for pipeline errors
after substitute overrides is called. We intend to call this from the
IR so this mean we need to validate parameters in the IR.

We may still need to do a follow up for Access/LVE/SVE too
(for OOB indices on non-constant arrays/vectors/matrices).

Bug: 382300469
Change-Id: I8fe1272e11f991f7047a327ce8692b9826604f3c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/219734
Reviewed-by: James Price <[email protected]>
Commit-Queue: Peter McNeeley <[email protected]>
  • Loading branch information
Peter McNeeley authored and Dawn LUCI CQ committed Dec 20, 2024
1 parent 88818a5 commit 7b0050f
Show file tree
Hide file tree
Showing 6 changed files with 1,247 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/tint/lang/core/ir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cc_library(
"builtin_call.cc",
"call.cc",
"clone_context.cc",
"const_param_validator.cc",
"constant.cc",
"construct.cc",
"continue.cc",
Expand Down Expand Up @@ -105,6 +106,7 @@ cc_library(
"builtin_call.h",
"call.h",
"clone_context.h",
"const_param_validator.h",
"constant.h",
"construct.h",
"continue.h",
Expand Down Expand Up @@ -188,6 +190,7 @@ cc_library(
"block_test.cc",
"break_if_test.cc",
"builder_test.cc",
"const_param_validator_test.cc",
"constant_test.cc",
"construct_test.cc",
"continue_test.cc",
Expand Down
3 changes: 3 additions & 0 deletions src/tint/lang/core/ir/BUILD.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ tint_add_target(tint_lang_core_ir lib
lang/core/ir/call.h
lang/core/ir/clone_context.cc
lang/core/ir/clone_context.h
lang/core/ir/const_param_validator.cc
lang/core/ir/const_param_validator.h
lang/core/ir/constant.cc
lang/core/ir/constant.h
lang/core/ir/construct.cc
Expand Down Expand Up @@ -194,6 +196,7 @@ tint_add_target(tint_lang_core_ir_test test
lang/core/ir/block_test.cc
lang/core/ir/break_if_test.cc
lang/core/ir/builder_test.cc
lang/core/ir/const_param_validator_test.cc
lang/core/ir/constant_test.cc
lang/core/ir/construct_test.cc
lang/core/ir/continue_test.cc
Expand Down
3 changes: 3 additions & 0 deletions src/tint/lang/core/ir/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ libtint_source_set("ir") {
"call.h",
"clone_context.cc",
"clone_context.h",
"const_param_validator.cc",
"const_param_validator.h",
"constant.cc",
"constant.h",
"construct.cc",
Expand Down Expand Up @@ -189,6 +191,7 @@ if (tint_build_unittests) {
"block_test.cc",
"break_if_test.cc",
"builder_test.cc",
"const_param_validator_test.cc",
"constant_test.cc",
"construct_test.cc",
"continue_test.cc",
Expand Down
317 changes: 317 additions & 0 deletions src/tint/lang/core/ir/const_param_validator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
// 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/const_param_validator.h"

#include <cstdint>
#include <string>
#include <utility>

#include "src/tint/lang/core/builtin_fn.h"
#include "src/tint/lang/core/intrinsic/table.h"
#include "src/tint/lang/core/ir/constant.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/instruction.h"
#include "src/tint/lang/core/ir/instruction_result.h"
#include "src/tint/lang/core/ir/module.h"
#include "src/tint/lang/core/ir/switch.h"
#include "src/tint/lang/core/ir/type/array_count.h"
#include "src/tint/lang/core/ir/value.h"
#include "src/tint/lang/core/number.h"
#include "src/tint/lang/core/type/type.h"
#include "src/tint/utils/diagnostic/diagnostic.h"
#include "src/tint/utils/internal_limits.h"
#include "src/tint/utils/result/result.h"
#include "src/tint/utils/rtti/switch.h"

namespace tint::core::ir {

namespace {

/// The core IR const param validator.
class ConstParamValidator {
public:
/// Create a core const param validator
/// @param mod the module to be validated
explicit ConstParamValidator(Module& mod);

/// Destructor
~ConstParamValidator();

/// Runs the const param validator over the module provided during construction
/// @returns success or failure
Result<SuccessType> Run();

void CheckSubgroupCall(const CoreBuiltinCall* call);
void CheckBuiltinCall(const BuiltinCall* call);
void CheckCoreBinaryCall(const CoreBinary* inst);
void CheckExtractBitsCall(const CoreBuiltinCall* call);
void CheckInsertBitsCall(const CoreBuiltinCall* call);
void CheckLdexpCall(const CoreBuiltinCall* call);
void CheckQuantizeToF16(const CoreBuiltinCall* call);
void CheckClampCall(const CoreBuiltinCall* call);
void CheckPack2x16float(const CoreBuiltinCall* call);
void CheckSmoothstepCall(const CoreBuiltinCall* call);
void CheckBinaryDivModCall(const CoreBinary* call);
void CheckBinaryShiftCall(const CoreBinary* call);

diag::Diagnostic& AddError(const Instruction& inst);

private:
Module& mod_;
constant::Eval const_eval_;
diag::List diagnostics_;
};

ConstParamValidator::ConstParamValidator(Module& mod)
: mod_(mod), const_eval_(mod.constant_values, diagnostics_) {}

ConstParamValidator::~ConstParamValidator() = default;

diag::Diagnostic& ConstParamValidator::AddError(const Instruction& inst) {
auto src = mod_.SourceOf(&inst);
return diagnostics_.AddError(src);
}

const constant::Value* GetConstArg(const CoreBuiltinCall* call, uint32_t param_index) {
if (call->Args().Length() <= param_index) {
return nullptr;
}
if (call->Args()[param_index] == nullptr) {
return nullptr;
}
return call->Args()[param_index]->As<ir::Constant>()->Value();
}

void ConstParamValidator::CheckExtractBitsCall(const CoreBuiltinCall* call) {
// This can be u32/i32 or vector of those types.
auto* param0 = call->Args()[0];
auto* const_val_offset = GetConstArg(call, 1);
auto* const_val_count = GetConstArg(call, 2);
if (const_val_count && const_val_offset) {
auto* zero = const_eval_.Zero(param0->Type(), {}, Source{}).Get();
auto fakeArgs = Vector{zero, const_val_offset, const_val_count};
[[maybe_unused]] auto result =
const_eval_.extractBits(param0->Type(), fakeArgs, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckInsertBitsCall(const CoreBuiltinCall* call) {
// This can be u32/i32 or vector of those types.
auto* param0 = call->Args()[0];
auto* const_val_offset = GetConstArg(call, 2);
auto* const_val_count = GetConstArg(call, 3);
if (const_val_count && const_val_offset) {
auto* zero = const_eval_.Zero(param0->Type(), {}, Source{}).Get();
auto fakeArgs = Vector{zero, zero, const_val_offset, const_val_count};
[[maybe_unused]] auto result =
const_eval_.insertBits(param0->Type(), fakeArgs, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckLdexpCall(const CoreBuiltinCall* call) {
auto* param0 = call->Args()[0];
if (auto const_val = GetConstArg(call, 1)) {
auto* zero = const_eval_.Zero(param0->Type(), {}, Source{}).Get();
auto fakeArgs = Vector{zero, const_val};
[[maybe_unused]] auto result =
const_eval_.ldexp(param0->Type(), fakeArgs, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckQuantizeToF16(const CoreBuiltinCall* call) {
if (auto const_val = GetConstArg(call, 0)) {
[[maybe_unused]] auto result = const_eval_.quantizeToF16(
call->Result(0)->Type(), Vector{const_val}, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckPack2x16float(const CoreBuiltinCall* call) {
if (auto const_val = GetConstArg(call, 0)) {
[[maybe_unused]] auto result = const_eval_.pack2x16float(
call->Result(0)->Type(), Vector{const_val}, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckSubgroupCall(const CoreBuiltinCall* call) {
if (auto const_val = GetConstArg(call, 1)) {
auto as_aint = const_val->ValueAs<AInt>();
// User friendly param name.
std::string paramName = "sourceLaneIndex";
switch (call->Func()) {
case core::BuiltinFn::kSubgroupShuffleXor:
paramName = "mask";
break;
case core::BuiltinFn::kSubgroupShuffleUp:
case core::BuiltinFn::kSubgroupShuffleDown:
paramName = "delta";
break;
default:
break;
}

if (as_aint >= tint::internal_limits::kMaxSubgroupSize) {
AddError(*call) << "The " << paramName << " argument of " << call->FriendlyName()
<< " must be less than " << tint::internal_limits::kMaxSubgroupSize;
} else if (as_aint < 0) {
AddError(*call) << "The " << paramName << " argument of " << call->FriendlyName()
<< " must be greater than or equal to zero";
}
}
}

void ConstParamValidator::CheckClampCall(const CoreBuiltinCall* call) {
auto* const_val_low = GetConstArg(call, 1);
auto* const_val_high = GetConstArg(call, 2);
if (const_val_low && const_val_high) {
auto fakeArgs = Vector{const_val_low, const_val_low, const_val_high};
[[maybe_unused]] auto result =
const_eval_.clamp(call->Result(0)->Type(), fakeArgs, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckSmoothstepCall(const CoreBuiltinCall* call) {
auto* const_val_low = GetConstArg(call, 1);
auto* const_val_high = GetConstArg(call, 2);
if (const_val_low && const_val_high) {
auto fakeArgs = Vector{const_val_low, const_val_high, const_val_high};
[[maybe_unused]] auto result =
const_eval_.smoothstep(call->Result(0)->Type(), fakeArgs, mod_.SourceOf(call));
}
}

void ConstParamValidator::CheckBuiltinCall(const BuiltinCall* inst) {
if (auto* call = inst->As<core::ir::CoreBuiltinCall>()) {
switch (call->Func()) {
case core::BuiltinFn::kSubgroupShuffle:
case core::BuiltinFn::kSubgroupShuffleXor:
case core::BuiltinFn::kSubgroupShuffleUp:
case core::BuiltinFn::kSubgroupShuffleDown:
CheckSubgroupCall(call);
break;
case core::BuiltinFn::kExtractBits:
CheckExtractBitsCall(call);
break;
case core::BuiltinFn::kInsertBits:
CheckInsertBitsCall(call);
break;
case core::BuiltinFn::kLdexp:
CheckLdexpCall(call);
break;
case core::BuiltinFn::kClamp:
CheckClampCall(call);
break;
case core::BuiltinFn::kSmoothstep:
CheckSmoothstepCall(call);
break;
case core::BuiltinFn::kQuantizeToF16:
CheckQuantizeToF16(call);
break;
case core::BuiltinFn::kPack2X16Float:
CheckPack2x16float(call);
break;
default:
break;
}
}
}

void ConstParamValidator::CheckBinaryDivModCall(const CoreBinary* call) {
// Integer division by zero should be checked for the partial evaluation case (only rhs
// is const). FP division by zero is only invalid when the whole expression is
// constant-evaluated.
if (call->RHS()->Type()->IsIntegerScalarOrVector()) {
auto rhs_constant = call->RHS()->As<ir::Constant>();
if (rhs_constant->Value()->AnyZero()) {
AddError(*call) << "integer division by zero is invalid";
}
}
}

void ConstParamValidator::CheckBinaryShiftCall(const CoreBinary* call) {
// If lhs value is a concrete type, and rhs is a const-expression greater than or equal
// to the bit width of lhs, then it is a shader-creation error.
const auto* elem_type = call->LHS()->Type()->DeepestElement();
const uint32_t bit_width = elem_type->Size() * 8;
if (auto* rhs_val_as_const = call->RHS()->As<ir::Constant>()) {
auto* rhs_as_value = rhs_val_as_const->Value();
for (size_t i = 0, n = rhs_as_value->NumElements(); i < n; i++) {
auto* shift_val = n == 1 ? rhs_as_value : rhs_as_value->Index(i);
if (shift_val->ValueAs<u32>() >= bit_width) {
AddError(*call) << "shift "
<< (call->Op() == core::BinaryOp::kShiftLeft ? "left" : "right")
<< " value must be less than the bit width of the lhs, which is "
<< bit_width;
break;
}
}
}
}

void ConstParamValidator::CheckCoreBinaryCall(const CoreBinary* call) {
switch (call->Op()) {
case core::BinaryOp::kDivide:
case core::BinaryOp::kModulo:
CheckBinaryDivModCall(call);
break;
case core::BinaryOp::kShiftLeft:
case core::BinaryOp::kShiftRight:
CheckBinaryShiftCall(call);
break;
default:
break;
}
}

Result<SuccessType> ConstParamValidator::Run() {
auto instructions = this->mod_.Instructions();

for (auto inst : instructions) {
tint::Switch(
inst, //
[&](const BuiltinCall* c) { CheckBuiltinCall(c); }, //
[&](const CoreBinary* c) { CheckCoreBinaryCall(c); }, //
[&](Default) {});
}

if (diagnostics_.ContainsErrors()) {
return Failure{std::move(diagnostics_)};
}

return Success;
}

} // namespace

Result<SuccessType> ValidateConstParam(Module& mod) {
ConstParamValidator v(mod);
auto ret = v.Run();
return ret;
}

} // namespace tint::core::ir
Loading

0 comments on commit 7b0050f

Please sign in to comment.