[ir] Add `ir::Builtin`

This CL adds an `ir::Builtin` which holds the builtin function
information and arguments for a builtin call.

Bug: tint:1718
Change-Id: If08df9f7a9f7edd2781f82d4a4955635290f3f9a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/122607
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2023-03-13 15:42:12 +00:00 committed by Dawn LUCI CQ
parent 286ac27fcc
commit 79d0f2b784
7 changed files with 130 additions and 3 deletions

View File

@ -703,6 +703,8 @@ if(${TINT_BUILD_IR})
ir/builder.h
ir/builder_impl.cc
ir/builder_impl.h
ir/builtin.cc
ir/builtin.h
ir/call.cc
ir/call.h
ir/constant.cc

View File

@ -193,4 +193,10 @@ ir::Construct* Builder::Construct(const type::Type* to, utils::VectorRef<Value*>
return ir.instructions.Create<ir::Construct>(Temp(to), std::move(args));
}
ir::Builtin* Builder::Builtin(const type::Type* type,
builtin::Function func,
utils::VectorRef<Value*> args) {
return ir.instructions.Create<ir::Builtin>(Temp(type), func, args);
}
} // namespace tint::ir

View File

@ -20,6 +20,7 @@
#include "src/tint/constant/scalar.h"
#include "src/tint/ir/binary.h"
#include "src/tint/ir/bitcast.h"
#include "src/tint/ir/builtin.h"
#include "src/tint/ir/constant.h"
#include "src/tint/ir/construct.h"
#include "src/tint/ir/convert.h"
@ -304,6 +305,15 @@ class Builder {
/// @returns the instruction
ir::Construct* Construct(const type::Type* to, utils::VectorRef<Value*> args);
/// Creates a builtin call instruction
/// @param type the return type
/// @param func the builtin function
/// @param args the arguments to be converted
/// @returns the instruction
ir::Builtin* Builtin(const type::Type* type,
builtin::Function func,
utils::VectorRef<Value*> args);
/// @returns a unique temp id
Temp::Id AllocateTempId();

View File

@ -791,9 +791,8 @@ utils::Result<Value*> BuilderImpl::EmitCall(const ast::CallExpression* expr) {
Instruction* instr = nullptr;
// If this is a builtin function, emit the specific builtin value
if (sem->Target()->As<sem::Builtin>()) {
// TODO(dsinclair): .. something ...
add_error(expr->source, "missing builtin function support");
if (auto* b = sem->Target()->As<sem::Builtin>()) {
instr = builder.Builtin(ty, b->Type(), args);
} else if (sem->Target()->As<sem::ValueConstructor>()) {
instr = builder.Construct(ty, std::move(args));
} else if (auto* conv = sem->Target()->As<sem::ValueConversion>()) {

View File

@ -1943,5 +1943,21 @@ FunctionEnd
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Builtin) {
auto i = GlobalVar("i", builtin::AddressSpace::kPrivate, Expr(1_f));
auto* expr = Call("asin", i);
WrapInFunction(expr);
auto& b = CreateBuilder();
InjectFlowBlock();
auto r = b.EmitExpression(expr);
ASSERT_TRUE(r) << b.error();
Disassembler d(b.builder.ir);
d.EmitBlockInstructions(b.current_flow_block->As<ir::Block>());
EXPECT_EQ(d.AsString(), R"(%2 (f32) = asin(%1 (void))
)");
}
} // namespace
} // namespace tint::ir

37
src/tint/ir/builtin.cc Normal file
View File

@ -0,0 +1,37 @@
// Copyright 2023 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/ir/builtin.h"
#include "src/tint/debug.h"
TINT_INSTANTIATE_TYPEINFO(tint::ir::Builtin);
// \cond DO_NOT_DOCUMENT
namespace tint::ir {
Builtin::Builtin(Value* result, builtin::Function func, utils::VectorRef<Value*> args)
: Base(result, args), func_(func) {}
Builtin::~Builtin() = default;
utils::StringStream& Builtin::ToString(utils::StringStream& out, const SymbolTable& st) const {
Result()->ToString(out, st);
out << " = " << builtin::str(func_) << "(";
EmitArgs(out, st);
out << ")";
return out;
}
} // namespace tint::ir
// \endcond

57
src/tint/ir/builtin.h Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2023 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_TINT_IR_BUILTIN_H_
#define SRC_TINT_IR_BUILTIN_H_
#include "src/tint/builtin/function.h"
#include "src/tint/castable.h"
#include "src/tint/ir/call.h"
#include "src/tint/symbol_table.h"
#include "src/tint/type/type.h"
#include "src/tint/utils/string_stream.h"
namespace tint::ir {
/// A value conversion instruction in the IR.
class Builtin : public Castable<Builtin, Call> {
public:
/// Constructor
/// @param result the result value
/// @param func the builtin function
/// @param args the conversion arguments
Builtin(Value* result, builtin::Function func, utils::VectorRef<Value*> args);
Builtin(const Builtin& instr) = delete;
Builtin(Builtin&& instr) = delete;
~Builtin() override;
Builtin& operator=(const Builtin& instr) = delete;
Builtin& operator=(Builtin&& instr) = delete;
/// @returns the builtin function
builtin::Function Func() const { return func_; }
/// Write the instruction to the given stream
/// @param out the stream to write to
/// @param st the symbol table
/// @returns the stream
utils::StringStream& ToString(utils::StringStream& out, const SymbolTable& st) const override;
private:
const builtin::Function func_;
};
} // namespace tint::ir
#endif // SRC_TINT_IR_BUILTIN_H_