Skip to content

Commit

Permalink
[Local] Preserve !invariant.load of dominating instruction
Browse files Browse the repository at this point in the history
Per LangRef:

> If a load instruction tagged with the !invariant.load metadata
> is executed, the memory location referenced by the load has to
> contain the same value at all points in the program where the
> memory location is dereferenceable; otherwise, the behavior is
> undefined.

As invariant.load violation is immediate undefined behavior, it
is sufficient for it to be present on the dominating load (for
the case where K does not move).
  • Loading branch information
nikic committed Apr 3, 2023
1 parent d527ace commit d68800d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2682,8 +2682,10 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD));
break;
case LLVMContext::MD_invariant_load:
// Only set the !invariant.load if it is present in both instructions.
K->setMetadata(Kind, JMD);
// If K moves, only set the !invariant.load if it is present in both
// instructions.
if (DoesKMove)
K->setMetadata(Kind, JMD);
break;
case LLVMContext::MD_nonnull:
if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/GVN/PRE/invariant-load.ll
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ define i32 @metadata_preservation(ptr nocapture %p, ptr nocapture %q) {
; CHECK-LABEL: define i32 @metadata_preservation
; CHECK-SAME: (ptr nocapture [[P:%.*]], ptr nocapture [[Q:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P]], align 4
; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P]], align 4, !invariant.load !0
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[X]], [[X]]
; CHECK-NEXT: ret i32 [[ADD]]
;
Expand Down

0 comments on commit d68800d

Please sign in to comment.