[OpenMP][FIX] Replace ICVs only with values valid at the getter position

While we might know the value if an ICV at a getter position it is not
always clear that we can simply use it. Verify the value is valid first
to avoid invalid IR.

Fixes #53300.
This commit is contained in:
Johannes Doerfert 2022-01-19 15:47:50 -06:00
parent ef95d45138
commit b4a7559844
2 changed files with 65 additions and 9 deletions

View File

@ -2166,7 +2166,7 @@ struct AAICVTrackerFunction : public AAICVTracker {
};
auto CallCheck = [&](Instruction &I) {
Optional<Value *> ReplVal = getValueForCall(A, &I, ICV);
Optional<Value *> ReplVal = getValueForCall(A, I, ICV);
if (ReplVal.hasValue() &&
ValuesMap.insert(std::make_pair(&I, *ReplVal)).second)
HasChanged = ChangeStatus::CHANGED;
@ -2192,12 +2192,12 @@ struct AAICVTrackerFunction : public AAICVTracker {
return HasChanged;
}
/// Hepler to check if \p I is a call and get the value for it if it is
/// Helper to check if \p I is a call and get the value for it if it is
/// unique.
Optional<Value *> getValueForCall(Attributor &A, const Instruction *I,
Optional<Value *> getValueForCall(Attributor &A, const Instruction &I,
InternalControlVar &ICV) const {
const auto *CB = dyn_cast<CallBase>(I);
const auto *CB = dyn_cast<CallBase>(&I);
if (!CB || CB->hasFnAttr("no_openmp") ||
CB->hasFnAttr("no_openmp_routines"))
return None;
@ -2213,8 +2213,8 @@ struct AAICVTrackerFunction : public AAICVTracker {
if (CalledFunction == GetterRFI.Declaration)
return None;
if (CalledFunction == SetterRFI.Declaration) {
if (ICVReplacementValuesMap[ICV].count(I))
return ICVReplacementValuesMap[ICV].lookup(I);
if (ICVReplacementValuesMap[ICV].count(&I))
return ICVReplacementValuesMap[ICV].lookup(&I);
return nullptr;
}
@ -2226,8 +2226,11 @@ struct AAICVTrackerFunction : public AAICVTracker {
const auto &ICVTrackingAA = A.getAAFor<AAICVTracker>(
*this, IRPosition::callsite_returned(*CB), DepClassTy::REQUIRED);
if (ICVTrackingAA.isAssumedTracked())
return ICVTrackingAA.getUniqueReplacementValue(ICV);
if (ICVTrackingAA.isAssumedTracked()) {
Optional<Value *> URV = ICVTrackingAA.getUniqueReplacementValue(ICV);
if (!URV || (*URV && AA::isValidAtPosition(**URV, I, OMPInfoCache)))
return URV;
}
// If we don't know, assume it changes.
return nullptr;
@ -2279,7 +2282,7 @@ struct AAICVTrackerFunction : public AAICVTracker {
break;
}
Optional<Value *> NewReplVal = getValueForCall(A, CurrInst, ICV);
Optional<Value *> NewReplVal = getValueForCall(A, *CurrInst, ICV);
if (!NewReplVal.hasValue())
continue;

View File

@ -0,0 +1,53 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature
; RUN: opt -S -openmp-opt-cgscc < %s | FileCheck %s
; RUN: opt -S -passes=openmp-opt-cgscc < %s | FileCheck %s
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
@inofth = external global i32, align 4
define void @set() {
; CHECK-LABEL: define {{[^@]+}}@set() {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* @inofth, align 4
; CHECK-NEXT: call void @omp_set_num_threads(i32 noundef [[TMP0]])
; CHECK-NEXT: ret void
;
entry:
%0 = load i32, i32* @inofth, align 4
call void @omp_set_num_threads(i32 noundef %0)
ret void
}
define i32 @test(i32 %a) {
; CHECK-LABEL: define {{[^@]+}}@test
; CHECK-SAME: (i32 [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @set()
; CHECK-NEXT: [[CALL:%.*]] = call i32 @omp_get_max_threads()
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[CALL]], [[A]]
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i32 0
; CHECK: if.end:
; CHECK-NEXT: ret i32 1
;
entry:
call void @set()
%call = call i32 @omp_get_max_threads()
%cmp = icmp ne i32 %call, %a
br i1 %cmp, label %if.then, label %if.end
if.then: ; preds = %entry
ret i32 0
if.end: ; preds = %if.then, %entry
ret i32 1
}
declare void @omp_set_num_threads(i32 noundef)
declare i32 @omp_get_max_threads()
!llvm.module.flags = !{!0}
!0 = !{i32 7, !"openmp", i32 50}