This is an archive of the discontinued LLVM Phabricator instance.

Honor the fwrapv option when generating the inbounds GEP .
Needs RevisionPublic

Authored by umesh.kalappa0 on Jan 30 2023, 12:19 AM.

Details

Reviewers
nikic
Summary

Fix for the issue :https://github.com/llvm/llvm-project/issues/59580

Where we don't emit the inbounds flag for the GEP when wrapv is enabled .

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJan 30 2023, 12:19 AM
umesh.kalappa0 requested review of this revision.Jan 30 2023, 12:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 30 2023, 12:19 AM
nikic requested changes to this revision.EditedJan 30 2023, 3:30 AM

CreateConstArrayGEP currently always creates an inbounds GEP. You need to modify it make inbounds optional or use a different method, not suppress it in the constant folding infrastructure.

This revision now requires changes to proceed.Jan 30 2023, 3:30 AM
nikic added a comment.Jan 30 2023, 3:39 AM

You also shouldn't need to introduce any new handling for the fwrapv option. I'm not sure how exactly it is currently threaded through, but there must be existing handling for it already. For example, there is isSignedOverflowDefined() on LangOptions.

umesh.kalappa0 edited the summary of this revision. (Show Details)

You also shouldn't need to introduce any new handling for the fwrapv option. I'm not sure how exactly it is currently threaded through, but there must be existing handling for it already. For example, there is isSignedOverflowDefined() on LangOptions.

Make sense here .

CreateConstArrayGEP currently always creates an inbounds GEP. You need to modify it make inbounds optional or use a different method, not suppress it in the constant folding infrastructure.

Thank you very much for the feedback and

We tried calling other overload like "CreateConstGEP" ,but that didn't help due to below call from "ConstantFoldGetElementPtr" ,that was recursive call from "ConstantExpr::getGetElementPtr" .

return ConstantExpr::getGetElementPtr(PointeeTy, C, Idxs, /*InBounds=*/true, InRangeIndex);

So we thought of disabling the call like "ConstantExpr::getGetElementPtr" with InBounds=true for wrapv and changes has its consensus so we end up changes that is shared for review.

nikic added a comment.Jan 30 2023, 6:32 AM

@umesh.kalappa0 I see. That part is actually a bug in LLVM. I'm working on a fix.

@umesh.kalappa0 This issue should be fixed by https://github.com/llvm/llvm-project/commit/5f01a626dd0615df49773d419c75aeb33666ee83. Can you please give it another try?

Thank you @nikic for the quick changes ,and updated the review

@nikic ,is that changes are ok ?

nikic added a comment.Feb 1 2023, 6:19 AM

I've found another bug in LLVM, fixed with https://github.com/llvm/llvm-project/commit/78f88082de3627ea04501c83a08f52cf1e60b4f7. After that change, the test case from https://github.com/llvm/llvm-project/issues/59580 appears to be handled correctly, it contains this in the final IR:

br i1 icmp ugt (ptr getelementptr ([0 x i8], ptr @end, i64 0, i64 536870911), ptr @end), label %if.then, label %if.else9

As such, I'm not sure if we actually need any change in Clang, as it seems like this was ultimately due to LLVM bugs. At least we would need a different test case, as the inbounds.c test case is already getting compiled correctly without this patch.

llvm/include/llvm/IR/ConstantFold.h
29

These changes shouldn't be needed anymore.

nikic requested changes to this revision.Mar 7 2023, 1:27 AM

The original issue has been fixed by the mentioned LLVM changes. As such, I don't think we need to actually do anything on the Clang side. Please let me know if you're still seeing issues in some case...

This revision now requires changes to proceed.Mar 7 2023, 1:27 AM