[tint] Add cache for unary and binary intrinsic look up For intrinsics that are referenced multiple times in a shader this avoids the expensive matching/lookup call. For the case in the referenced issue, this shaves off about 20% of the runtime. Bug: 563083232 Change-Id: I9b6a5962a1d5c59622805446542e9f3a4b63cd6e Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/343855 Auto-Submit: Ryan Harrison <rharrison@chromium.org> Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: dan sinclair <dsinclair@chromium.org> Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/core/intrinsic/table.h b/src/tint/lang/core/intrinsic/table.h index 7328d9e..807f9ce 100644 --- a/src/tint/lang/core/intrinsic/table.h +++ b/src/tint/lang/core/intrinsic/table.h
@@ -31,6 +31,7 @@ #include <memory> #include <ostream> #include <string> +#include <tuple> #include <utility> #include <variant> @@ -40,6 +41,7 @@ #include "src/tint/lang/core/intrinsic/ctor_conv.h" #include "src/tint/lang/core/intrinsic/table_data.h" #include "src/tint/lang/core/unary_op.h" +#include "src/tint/utils/containers/hashmap.h" #include "src/tint/utils/containers/vector.h" #include "src/tint/utils/text/string.h" #include "src/tint/utils/text/string_stream.h" @@ -330,7 +332,9 @@ Result<Overload, StyledText> Lookup(core::UnaryOp op, const core::type::Type* arg, EvaluationStage earliest_eval_stage) { - return LookupUnary(context, op, arg, earliest_eval_stage); + UnaryOpSig cache_key = std::make_tuple(op, arg, earliest_eval_stage); + return unary_cache_.GetOrAdd( + cache_key, [&] { return LookupUnary(context, op, arg, earliest_eval_stage); }); } /// Lookup looks for the binary op overload with the given signature, raising an error @@ -352,7 +356,10 @@ const core::type::Type* rhs, EvaluationStage earliest_eval_stage, bool is_compound) { - return LookupBinary(context, op, lhs, rhs, earliest_eval_stage, is_compound); + BinaryOpSig cache_key = std::make_tuple(op, lhs, rhs, earliest_eval_stage, is_compound); + return binary_cache_.GetOrAdd(cache_key, [&] { + return LookupBinary(context, op, lhs, rhs, earliest_eval_stage, is_compound); + }); } /// Lookup looks for the value constructor or conversion overload for the given CtorConv. @@ -378,6 +385,18 @@ /// The intrinsic context Context context; + + /// Cache for unary operator overloads + using UnaryOpSig = std::tuple<core::UnaryOp, const core::type::Type*, EvaluationStage>; + Hashmap<UnaryOpSig, Result<Overload, StyledText>, 16> unary_cache_; + + /// Cache for binary operator overloads + using BinaryOpSig = std::tuple<core::BinaryOp, + const core::type::Type*, + const core::type::Type*, + EvaluationStage, + bool>; + Hashmap<BinaryOpSig, Result<Overload, StyledText>, 16> binary_cache_; }; } // namespace tint::core::intrinsic