Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support llvm.frexp intrinsic translation #2252

Merged
merged 8 commits into from
Jan 29, 2024
Merged

Conversation

vmaksimo
Copy link
Contributor

@vmaksimo vmaksimo commented Dec 5, 2023

Map @llvm.frexp intrinsic to OpenCL Extended Instruction frexp builtin.

The difference in signatures and return values is covered by extracting/combining values from and into composite type.

LLVM IR:
{ float %fract, i32 %exp }  @llvm.frexp.f32.i32(float %val)
SPIR-V:
{ float %fract } ExtInst frexp (float %val, i32 %exp)

This change also fixes reverse translation of non-constant values of
`OpCompositeConstruct`.
@vmaksimo vmaksimo requested a review from MrSidims December 5, 2023 13:54
@vmaksimo
Copy link
Contributor Author

vmaksimo commented Dec 6, 2023

Please take a look @MrSidims @asudarsa @LU-JOHN @bwlodarcz @VyacheslavLevytskyy

@MrSidims
Copy link
Contributor

MrSidims commented Dec 6, 2023

This change also fixes reverse translation of non-constant values of OpCompositeConstruct.

There is a value to put it in a separate PR. Reason is that the bug fix alone is a good candidate to be backported to earlier release branches

@vmaksimo
Copy link
Contributor Author

vmaksimo commented Dec 6, 2023

There is a value to put it in a separate PR. Reason is that the bug fix alone is a good candidate to be backported to earlier release branches

I'll try to come up with a PRs split.

lib/SPIRV/SPIRVReader.cpp Outdated Show resolved Hide resolved
@vmaksimo vmaksimo requested a review from asudarsa December 18, 2023 10:16
@asudarsa
Copy link
Contributor

Please take a look @LU-JOHN @bwlodarcz @VyacheslavLevytskyy

Thanks

@bwlodarcz
Copy link
Contributor

During review I checked what will happen when:

define { float, i1 } @frexp_bool() {
  %ret = call { float, i1 } @llvm.frexp.f32.i1(float -0.0)
  ret { float, i1 } %ret
}

which is correct llvm IR.
It appears that the result SPIRV seems ok:

7 Decorate 6 LinkageAttributes "frexp_bool" Export
...

4 TypeInt 15 32 0
3 TypeFloat 3 32
2 TypeBool 4
4 TypeStruct 2 3 4

3 TypeFunction 5 2
4 TypePointer 8 7 4
4 TypeStruct 14 3 15
....

5 Function 2 6 0 5

2 Label 7
4 Variable 8 9 7
7 ExtInst 3 11 1 frexp 10 9
4 Load 4 12 9
5 CompositeConstruct 2 13 11 12
2 ReturnValue 13

1 FunctionEnd

but looking on OpenCL extension spec:
https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html

halfn frexp (halfn x, __global intn *exp)
half frexp (half x, __global int *exp)

halfn frexp (halfn x, __local intn *exp)
half frexp (half x, __local int *exp)

halfn frexp (halfn x, __private intn *exp)
half frexp (half x, __private int *exp)

For OpenCL C 2.0 or with the __opencl_c_generic_address_space feature macro:

halfn frexp (halfn x, intn *exp)
half frexp (half x, int *exp)

Is this correct? I mean can we pass BoolType pointer to OpenCL frexp and expect to be handled correctly?

@MrSidims
Copy link
Contributor

MrSidims commented Jan 8, 2024

Is this correct? I mean can we pass BoolType pointer to OpenCL frexp and expect to be handled correctly?

No, we mustn't expect that. And I do believe, that it is LLVM bug - it makes a little sense for frexp to store integral exponent of a float to i1 (unless you want to support mini-floats, but it's not part of LLVM and it's unlikely to be inherited from just float).

I suggest that we add here an assertion (or an error, but IMHO assertion here is better) to check if input integer is not i1. For the rest integer types we should add conversion to i32 by UConvert to obey the spec https://registry.khronos.org/SPIR-V/specs/1.0/OpenCL.ExtendedInstructionSet.100.mobile.html "exp must be a pointer(global, local, private, generic) to i32 or vector(2,3,4,8,16) of i32 values".

@bwlodarcz
Copy link
Contributor

Is this correct? I mean can we pass BoolType pointer to OpenCL frexp and expect to be handled correctly?

No, we mustn't expect that. And I do believe, that it is LLVM bug - it makes a little sense for frexp to store integral exponent of a float to i1 (unless you want to support mini-floats, but it's not part of LLVM and it's unlikely to be inherited from just float).

I suggest that we add here an assertion (or an error, but IMHO assertion here is better) to check if input integer is not i1. For the rest integer types we should add conversion to i32 by UConvert to obey the spec https://registry.khronos.org/SPIR-V/specs/1.0/OpenCL.ExtendedInstructionSet.100.mobile.html "exp must be a pointer(global, local, private, generic) to i32 or vector(2,3,4,8,16) of i32 values".

Why just not use everywhere i32 and just trunc or zext exponent result?

SPIRVTypePointer *ITy = static_cast<SPIRVTypePointer *>(transPointerType(
II->getType()->getStructElementType(1), SPIRAS_Private));

assert(ITy->getElementType()->getBitWidth() == 32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check in conformance with comments? Do we want to assert out if type is i16?
Also two other comments:

  1. Can we pls add a test for this?
  2. Can we add a error message here?

Thanks

Copy link
Contributor Author

@vmaksimo vmaksimo Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to assert out if type is i16?

Yes, because the intrinsic is used only with i32 by OpenCL Ext Inst set specification. And actually LLVM language reference also allows only i32 here.

  1. Can we pls add a test for this?

OK, I can add a separate negative test for that.

  1. Can we add a error message here?

I agree with Dmitry that the assertion is better in this case. Do you mind if I leave it as is?

Copy link
Contributor

@asudarsa asudarsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. few comments to address.

Thanks

@vmaksimo vmaksimo requested a review from asudarsa January 19, 2024 11:21
@vmaksimo vmaksimo closed this Jan 22, 2024
@vmaksimo vmaksimo reopened this Jan 22, 2024

; CHECK-SPIRV: ExtInstImport [[#ExtInstSetId:]] "OpenCL.std"

; CHECK-SPIRV: TypeInt [[#TypeInt:]] 32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make this CHECK-SPIRV-DAG in case the order changes in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about that - I don't think that types would be shuffled.
I guess we should proceed as is and add DAG checks only if the ordering problem appears.

Copy link
Contributor

@LU-JOHN LU-JOHN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MrSidims
Copy link
Contributor

Restarting CI, merging afterwards

@MrSidims MrSidims closed this Jan 29, 2024
@MrSidims MrSidims reopened this Jan 29, 2024
@MrSidims MrSidims changed the title Support frexp intrinsic translation Support llvm.frexp intrinsic translation Jan 29, 2024
@MrSidims MrSidims merged commit e8b2018 into KhronosGroup:main Jan 29, 2024
23 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants