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

Regularize LLVM code to remove use of non standard integer types in fptoui and fptosi intrinsics #2500

Merged
merged 10 commits into from
Apr 16, 2024

Conversation

asudarsa
Copy link
Contributor

It is possible that incoming LLVM IR conversion instructions convert floating point to non-standard integer types.
For e.g. %0 = call i2 @llvm.fptosi.sat.i2.f32(float %input)
Such types are not supported in SPIR-V. This PR cleans up such code and removes occurrence of non-standard integer types.

Thanks

…ptoui and fptosi intrinsics

Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
@asudarsa
Copy link
Contributor Author

Please review once marked ready. Thanks

Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
@asudarsa asudarsa marked this pull request as ready for review April 11, 2024 10:52
@asudarsa
Copy link
Contributor Author

This PR tries to address a very limited set of issues found due to use of non-standard integer types. A more rounded solution may be required in the long run. Tracked here: #481

Thanks

Copy link
Contributor

@maksimsab maksimsab left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@maarquitos14 maarquitos14 left a comment

Choose a reason for hiding this comment

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

LGTM.

Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
@asudarsa asudarsa requested a review from LU-JOHN April 11, 2024 16:12
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
Copy link
Contributor

Choose a reason for hiding this comment

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

Data layout doesn't match the input LLVM IR ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will fix in upcoming patch. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked at the original testcase. Data layout is exactly the same as it is here. Can you please let me know how it does not match the input LLVM IR? Thanks

Copy link
Contributor

@MrSidims MrSidims Apr 12, 2024

Choose a reason for hiding this comment

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

n8:16:32:64

There is no "2" and "4" which would stand for i2 and i4. Which means, that the test case is either written artificially or generated using some hacks. If the later wouldn't it makes sense to investigate frontend hacks instead of changing translation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or if not hacks, may be just aggressive inst combine should respect data layout?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I mentioned earlier, the test case is a 'modified' version of an actual LLVM IR file created using upstream LLVM compiler (LLVM.org). I have not changed the datalayout or the instructions related to 'i2' uses.
I am trying to figure out if this might be the case of the optimization not handling data layout correctly.
I am however surprised that 'llvm-as' does not complain about this LLVM IR. And I am also sure the verifier code runs at some time during front-end compilation. However, it is quite possible that they do not try to 'verify' datalayout related issues.

I went a bit deeper. i see that there is a helper function 'isLegalInteger' in the datalayout. However, this particular optimization does not call this function to check for legality. I will try to get this answered in the community.

In the meanwhile, would it make sense to 'merge' this as is and then revert it if/when frontend fix is in place?

Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems the entries in datalayout are expected to be more of a guide that strict rules. for example, It is not documented anywhere that i2 is illegal if it is not listed in the target datalayout, thought it does sound counter-intuitive. I think we might have to assume that this LLVM IR is legal and proceed with the change here.

Thanks

Copy link

Choose a reason for hiding this comment

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

It seems the entries in datalayout are expected to be more of a guide that strict rules. for example, It is not documented anywhere that i2 is illegal if it is not listed in the target datalayout, thought it does sound counter-intuitive. I think we might have to assume that this LLVM IR is legal and proceed with the change here.

Thanks

I agree. The fact the datalayout field does not explicitly "mention" i2 doesn't prevent the LLVM optimizer to use i2 as an intermediat type during optimization IMO. The LLVM language spec simply states that the " data layout string that specifies how data is to be laid out in memory". There isn't anything that says using i2 is illegal.

lib/SPIRV/SPIRVRegularizeLLVM.cpp Show resolved Hide resolved
lib/SPIRV/SPIRVRegularizeLLVM.cpp Outdated Show resolved Hide resolved
lib/SPIRV/SPIRVRegularizeLLVM.cpp Outdated Show resolved Hide resolved
lib/SPIRV/SPIRVRegularizeLLVM.cpp Outdated Show resolved Hide resolved
test/llvm-intrinsics/fp_to_arbitrary_size_int_intrinsic.ll Outdated Show resolved Hide resolved
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
@asudarsa asudarsa requested review from svenvh and LU-JOHN April 12, 2024 14:31
@asudarsa asudarsa requested a review from MrSidims April 12, 2024 14:31
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
Signed-off-by: Sudarsanam, Arvind <arvind.sudarsanam@intel.com>
@asudarsa
Copy link
Contributor Author

Hi @svenvh and @MrSidims

Are there any more open issues/concerns? I have verified with LLVM documentation that generation of data types not mentioned in the datalayout is not explicitly called out as illegal. Please let me know if there is any evidence contrary to this.
If there are no concerns, can you please provide approvals when convenient?

Thanks a lot for your insightful feedbacks on this PR.
Sincerely

Copy link
Member

@svenvh svenvh left a comment

Choose a reason for hiding this comment

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

No more concerns from my side.

@asudarsa asudarsa merged commit 14301c2 into KhronosGroup:main Apr 16, 2024
9 checks passed
@asudarsa
Copy link
Contributor Author

Thanks everyone for the reviews.

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.

8 participants