Bug 200287 - [WHLSL] Make resolveFunction in Checker faster
Summary: [WHLSL] Make resolveFunction in Checker faster
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebGPU (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Saam Barati
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-07-30 16:25 PDT by Saam Barati
Modified: 2019-08-06 21:18 PDT (History)
9 users (show)

See Also:


Attachments
WIP (19.81 KB, patch)
2019-07-30 23:50 PDT, Saam Barati
no flags Details | Formatted Diff | Diff
WIP (16.65 KB, patch)
2019-08-06 12:27 PDT, Saam Barati
no flags Details | Formatted Diff | Diff
patch (16.92 KB, patch)
2019-08-06 17:26 PDT, Saam Barati
rmorisset: review+
Details | Formatted Diff | Diff
patch for landing (16.96 KB, patch)
2019-08-06 19:26 PDT, Saam Barati
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Saam Barati 2019-07-30 16:25:40 PDT
We spend 40ms(!!!) in it in compute_boids
Comment 1 Saam Barati 2019-07-30 23:50:14 PDT
Created attachment 375211 [details]
WIP
Comment 2 Saam Barati 2019-07-31 12:24:27 PDT
about ~25ms faster. But still not super fast
Comment 3 Saam Barati 2019-08-05 14:51:46 PDT
Gonna check this out
Comment 4 Saam Barati 2019-08-06 12:27:36 PDT
Created attachment 375642 [details]
WIP

rebased

Might not really be faster though
Comment 5 Saam Barati 2019-08-06 15:32:37 PDT
It is actually 6ms faster! Will prepare the patch
Comment 6 Saam Barati 2019-08-06 17:26:37 PDT
Created attachment 375671 [details]
patch
Comment 7 EWS Watchlist 2019-08-06 17:28:10 PDT
Attachment 375671 [details] did not pass style-queue:


ERROR: Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:526:  Extra space before last semicolon. If this should be an empty statement, use { } instead.  [whitespace/semicolon] [5]
ERROR: Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:599:  The parameter name "location" adds no information, so it should be removed.  [readability/parameter_name] [5]
Total errors found: 2 in 2 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 8 Saam Barati 2019-08-06 17:39:30 PDT
Comment on attachment 375671 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=375671&action=review

> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:526
> +                castReturnType = &downcast<AST::NamedType>(function.type().unifyNode()) ;

will fix style
Comment 9 Robin Morisset 2019-08-06 17:50:30 PDT
Comment on attachment 375671 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=375671&action=review

r=me with comments

> Source/WebCore/ChangeLog:15
> +        in the hash table such that they still allow constants to resolved to

to resolved => to be resolved

> Source/WebCore/ChangeLog:24
> +        The first two rules are because uint and int constants can be matched against

I think only int constants should be coerced to float/uint.
If you care enough about the type of a value to make it a uint literal (with the u at the end), doing any kind of silent coercion sounds iffy to me.
This only change this changelog, not the code (as int literals can become any of float/int/uint, the three types must be in the same equivalence class anyway).

> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:142
> +        for (auto& type : m_types)

That hash function does not look great to me: it treats foo(int, float*) the same as foo(float*, int). And if two arguments have the same type, then their hashes cancel each others, so foo(int, int) has the same hash as foo(bool, bool).
Can you rotate the hash of each argument type by its position in the list of arguments or something like that?

> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:146
> +            hash ^= WTF::PtrHash<AST::Type*>::hash(&m_castReturnType->unifyNode());

Why is this so different from the hashing of the argument types above?

> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:532
> +                types.uncheckedAppend(normalizedTypeForFunctionKey(*param->type()));

It probably isn't worth changing, but I wonder whether things might be simpler if normalizedTypeForFunctionKey was called inside FunctionKey.hash()/FunctionKey.operator==.
It would not only avoid this call, but most of resolveFunction below.

> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:636
> +    HashMap<FunctionKey, Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>, FunctionKey::Hash, FunctionKey::Traits> m_functions;

I'm curious, when do we need to put the hash and traits explicitly and when are they automatically inferred? I've seen that most HashMaps in our codebase don't have them explicit, but I am not sure when they can be omitted.
Comment 10 Saam Barati 2019-08-06 19:22:40 PDT
Comment on attachment 375671 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=375671&action=review

>> Source/WebCore/ChangeLog:24
>> +        The first two rules are because uint and int constants can be matched against
> 
> I think only int constants should be coerced to float/uint.
> If you care enough about the type of a value to make it a uint literal (with the u at the end), doing any kind of silent coercion sounds iffy to me.
> This only change this changelog, not the code (as int literals can become any of float/int/uint, the three types must be in the same equivalence class anyway).

Good point. Will reword it. (We might currently allow coercing unsigned literals to int literals, but I believe our plan is to prevent that.)

>> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:142
>> +        for (auto& type : m_types)
> 
> That hash function does not look great to me: it treats foo(int, float*) the same as foo(float*, int). And if two arguments have the same type, then their hashes cancel each others, so foo(int, int) has the same hash as foo(bool, bool).
> Can you rotate the hash of each argument type by its position in the list of arguments or something like that?

Good point. I'll just add the index to each argument's hash.

>> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:146
>> +            hash ^= WTF::PtrHash<AST::Type*>::hash(&m_castReturnType->unifyNode());
> 
> Why is this so different from the hashing of the argument types above?

Because m_castReturnType is a NamedType. A NamedType's identity is based on its unifyNode's pointer value

>> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:532
>> +                types.uncheckedAppend(normalizedTypeForFunctionKey(*param->type()));
> 
> It probably isn't worth changing, but I wonder whether things might be simpler if normalizedTypeForFunctionKey was called inside FunctionKey.hash()/FunctionKey.operator==.
> It would not only avoid this call, but most of resolveFunction below.

The main motivation for doing this here is avoiding calling it repeatedly during the hash lookup loop (for == and for hash()). I agree it's less elegant, but it should be more performant this way.

>> Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp:636
>> +    HashMap<FunctionKey, Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>, FunctionKey::Hash, FunctionKey::Traits> m_functions;
> 
> I'm curious, when do we need to put the hash and traits explicitly and when are they automatically inferred? I've seen that most HashMaps in our codebase don't have them explicit, but I am not sure when they can be omitted.

They're never really "automatically" inferred. If you don't specify them, we use the "default" hash and traits in WTF's namespace. And for most things, we've defined defaults.

To define a default, you can do something like this:
namespace WTF {

template<typename T> struct DefaultHash;
template<> struct DefaultHash<JSC::B3::Air::Tmp> {
    typedef JSC::B3::Air::TmpHash Hash;
};

template<typename T> struct HashTraits;
template<> struct HashTraits<JSC::B3::Air::Tmp> : SimpleClassHashTraits<JSC::B3::Air::Tmp> { };

} // namespace WTF
Comment 11 Saam Barati 2019-08-06 19:26:45 PDT
Created attachment 375675 [details]
patch for landing
Comment 12 WebKit Commit Bot 2019-08-06 21:17:35 PDT
Comment on attachment 375675 [details]
patch for landing

Clearing flags on attachment: 375675

Committed r248339: <https://trac.webkit.org/changeset/248339>
Comment 13 WebKit Commit Bot 2019-08-06 21:17:37 PDT
All reviewed patches have been landed.  Closing bug.
Comment 14 Radar WebKit Bug Importer 2019-08-06 21:18:17 PDT
<rdar://problem/54017979>