questions about implementing intrinsics
Doug Simon
doug.simon at oracle.com
Mon Dec 2 02:16:11 PST 2013
On Dec 1, 2013, at 6:52 PM, Venkatachalam, Vasanth <Vasanth.Venkatachalam at amd.com> wrote:
> Doug or others-
>
> I'm trying to understand the process of implementing intrinsics (for the HSAIL backend) for some java.lang.Math routines that x86 doesn't necessarily intrinsify.
> As a toy example, I'm trying to implement my own intrinsic for Math.sqrt( ). Below are the steps I tried to take to hack a small prototype.
> Can you tell me whether I'm on the right track? I also had some questions below.
>
>
> 1) Define a package com.oracle.graal.replacements.hsail.
>
> 2) In the above package, define a class MathSubstitutionsHSAIL.java (similar to MathSubstitutionsX86) which contains substitutions for methods of the java.lang.Math class.
>
> I currently just have a substitution for Math.sqrt( ), but I could list substitutions here for other java.lang.Math routines that aren't in MathSubstitutionsX86.
>
> 3) Define a MathIntrinsicHSAILNode (under com.oracle.graal.replacements.hsail) which is similar to the MathIntrinsicNode but defines the operations that will be treated as intrinsics in the HSAIL backend.
>
> a. The substitutions in MathSubsitutionsHSAIL.java will call the MathIntrinsicHSAILNode.compute( ) routine for the math operations we are interested in intrinsifying.
>
> 4) Define a ReplacementsProvider called GraalHSAILMethodSubstitutions.java under com.oracle.graal.replacements.hsail/ . Implement the registerReplacements method( ) to register specific replacements with the compiler. My version of registerReplacements() just calls replacements.registerSubstitutions(MathSubstitutionsHSAIL.class).
>
> 5) Override HSAILHotspotBackend.completeInitialization( ) to call ReplacementsProvider.registerReplacements( ) similar to the way it's being done in the host backend, so that the substitutions I have defined get registered in the compiler.
>
> Some questions:
>
>
> a) The part that's fuzzy to me is step 5). VMToCompilerImpl.startCompiler() is calling completeInitialization( ) for the host backend (x86) and for each of the GPU target backends (including HSAIL). The completeInitialization( ) routine in the host backend is calling ReplacementsProvider.registerReplacements() to register all the x86 replacements. Am I supposed to override HSAILHotSpotBackend.completeInitialization( ) to do something similar so that the HSAIL substitutions get registered? Can you explain this process a bit? I'm not clear on who is supposed to call ReplacementsProvider.registerReplacements() to register my intrinsics.
You need to do the registration yourself in HSAILHotspotBackend.completeInitialization(). However, you should not use the ServiceLoader mechanism that HotSpotHostBackend does. This means you don’t need a ReplacementsProvider (such as GraalHSAILMethodSubstitutions) at all. GPU-specific replacements should be explicitly registered with something like:
try (Scope s = Debug.scope("RegisterReplacements", new DebugDumpScope("RegisterReplacements"))) {
Replacements replacements = providers.getReplacements();
replacements.registerSubstitutions(MathSubstitutionsHSAIL.class)
}
> b) From looking at VMToCompilerImpl.startCompiler(), it appears that the substitutions used in the host backend (x86) as well as substitutions used in the GPU target backend (HSAIL) will all get registered with the compiler if I take the approach in a) above. How can I ensure that my math intrinsics are the ones that get used instead of the host backend intrinsics? In particular, how do I ensure that my intrinsic for Math.sqrt is what gets used (instead of the host backend's version) when I'm generating HSAIL code?
Replacements get registered with a backend specific Replacements object which will be a HSAILHotSpotReplacementsImpl object in your case. Since a compilation uses the Replacements object it was provided with (in the Providers value passed to GraalCompiler.compileGraph()), it will only get replacements from that object. You therefore just have to make sure HSAILHotSpotReplacementsImpl only returns the replacements that are applicable to HSAIL.
-Doug
More information about the graal-dev
mailing list