Skip to content

[llvm][OpenMP] Add implicit cast to omp.atomic.read #114659

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

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@ computeOpenMPScheduleType(ScheduleKind ClauseKind, bool HasChunks,
return Result;
}

/// Emit an implicit cast to convert \p XRead to type of variable \p V
static llvm::Value *emitImplicitCast(IRBuilder<> &Builder, llvm::Value *XRead,
llvm::Value *V) {
// TODO: Add this functionality to the `AtomicInfo` interface
llvm::Type *XReadType = XRead->getType();
llvm::Type *VType = V->getType();
if (llvm::AllocaInst *vAlloca = dyn_cast<llvm::AllocaInst>(V))
VType = vAlloca->getAllocatedType();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would AllocaInst::getAllocatedType be different to the value's type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the type of atomic pointer by default is ptr. We would like to know the real type (i.e. AllocatedType) in order to know whether we need a conversion or an extraction (in case of complex type).


if (XReadType->isStructTy() && VType->isStructTy())
// No need to extract or convert. A direct
// `store` will suffice.
return XRead;

if (XReadType->isStructTy())
XRead = Builder.CreateExtractValue(XRead, /*Idxs=*/0);
if (VType->isIntegerTy() && XReadType->isFloatingPointTy())
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think this can be else if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually if you consider the test case:

program atomic_array
  complex :: x(2)
  integer :: y

  x(1) = (1.0, 1.0)
  x(2) = (2.0, 2.0)
  !$omp parallel
    !$omp atomic read
      y = x(1)
  !$omp end parallel
  print *, y
end program

Then we need to extract the real component of complex into a float, and store it to integer. We thus need this check in place to ensure the conversion takes place.

XRead = Builder.CreateFPToSI(XRead, VType);
else if (VType->isFloatingPointTy() && XReadType->isIntegerTy())
XRead = Builder.CreateSIToFP(XRead, VType);
Comment on lines +284 to +286
Copy link
Member

Choose a reason for hiding this comment

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

An atomic read should not have implicit semantics of converting between int and fp. That could cause some trouble, e.g. rounding.

else if (VType->isIntegerTy() && XReadType->isIntegerTy())
XRead = Builder.CreateIntCast(XRead, VType, true);
else if (VType->isFloatingPointTy() && XReadType->isFloatingPointTy())
XRead = Builder.CreateFPCast(XRead, VType);
return XRead;
}

/// Make \p Source branch to \p Target.
///
/// Handles two situations:
Expand Down Expand Up @@ -8373,6 +8400,8 @@ OpenMPIRBuilder::createAtomicRead(const LocationDescription &Loc,
}
}
checkAndEmitFlushAfterAtomic(Loc, AO, AtomicKind::Read);
if (XRead->getType() != V.Var->getType())
XRead = emitImplicitCast(Builder, XRead, V.Var);
Comment on lines +8403 to +8404
Copy link
Member

Choose a reason for hiding this comment

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

The code above seems to already handle casting?

Builder.CreateStore(XRead, V.Var, V.IsVolatile);
return Builder.saveIP();
}
Expand Down Expand Up @@ -8657,6 +8686,8 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createAtomicCapture(
return AtomicResult.takeError();
Value *CapturedVal =
(IsPostfixUpdate ? AtomicResult->first : AtomicResult->second);
if (CapturedVal->getType() != V.Var->getType())
CapturedVal = emitImplicitCast(Builder, CapturedVal, V.Var);
Builder.CreateStore(CapturedVal, V.Var, V.IsVolatile);

checkAndEmitFlushAfterAtomic(Loc, AO, AtomicKind::Capture);
Expand Down
71 changes: 71 additions & 0 deletions mlir/test/Target/LLVMIR/openmp-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,77 @@ llvm.func @omp_atomic_read(%arg0 : !llvm.ptr, %arg1 : !llvm.ptr) -> () {

// -----

// CHECK-LABEL: @omp_atomic_read_implicit_cast
llvm.func @omp_atomic_read_implicit_cast () {
//CHECK: %[[Z:.*]] = alloca float, i64 1, align 4
//CHECK: %[[Y:.*]] = alloca double, i64 1, align 8
//CHECK: %[[X:.*]] = alloca [2 x { float, float }], i64 1, align 8
//CHECK: %[[W:.*]] = alloca i32, i64 1, align 4
//CHECK: %[[X_ELEMENT:.*]] = getelementptr { float, float }, ptr %3, i64 0
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x f32 {bindc_name = "z"} : (i64) -> !llvm.ptr
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x f64 {bindc_name = "y"} : (i64) -> !llvm.ptr
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x !llvm.array<2 x struct<(f32, f32)>> {bindc_name = "x"} : (i64) -> !llvm.ptr
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i32 {bindc_name = "w"} : (i64) -> !llvm.ptr
%8 = llvm.mlir.constant(1 : index) : i64
%9 = llvm.mlir.constant(2 : index) : i64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.mlir.constant(0 : i64) : i64
%12 = llvm.sub %8, %10 overflow<nsw> : i64
%13 = llvm.mul %12, %10 overflow<nsw> : i64
%14 = llvm.mul %13, %10 overflow<nsw> : i64
%15 = llvm.add %14, %11 overflow<nsw> : i64
%16 = llvm.mul %10, %9 overflow<nsw> : i64
%17 = llvm.getelementptr %5[%15] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(f32, f32)>

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = alloca { float, float }, align 8
//CHECK: call void @__atomic_load(i64 8, ptr %[[X_ELEMENT]], ptr %[[ATOMIC_LOAD_TEMP]], i32 0)
//CHECK: %[[LOAD:.*]] = load { float, float }, ptr %[[ATOMIC_LOAD_TEMP]], align 8
//CHECK: %[[EXT:.*]] = extractvalue { float, float } %[[LOAD]], 0
//CHECK: store float %[[EXT]], ptr %[[Y]], align 4
omp.atomic.read %3 = %17 : !llvm.ptr, !llvm.ptr, !llvm.struct<(f32, f32)>

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = load atomic i32, ptr %[[Z]] monotonic, align 4
//CHECK: %[[CAST:.*]] = bitcast i32 %[[ATOMIC_LOAD_TEMP]] to float
//CHECK: %[[LOAD:.*]] = fpext float %[[CAST]] to double
//CHECK: store double %[[LOAD]], ptr %[[Y]], align 8
omp.atomic.read %3 = %1 : !llvm.ptr, !llvm.ptr, f32

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = load atomic i32, ptr %[[W]] monotonic, align 4
//CHECK: %[[LOAD:.*]] = sitofp i32 %[[ATOMIC_LOAD_TEMP]] to double
//CHECK: store double %[[LOAD]], ptr %[[Y]], align 8
omp.atomic.read %3 = %7 : !llvm.ptr, !llvm.ptr, i32

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = load atomic i64, ptr %[[Y]] monotonic, align 4
//CHECK: %[[CAST:.*]] = bitcast i64 %[[ATOMIC_LOAD_TEMP]] to double
//CHECK: %[[LOAD:.*]] = fptrunc double %[[CAST]] to float
//CHECK: store float %[[LOAD]], ptr %[[Z]], align 4
omp.atomic.read %1 = %3 : !llvm.ptr, !llvm.ptr, f64

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = load atomic i32, ptr %[[W]] monotonic, align 4
//CHECK: %[[LOAD:.*]] = sitofp i32 %[[ATOMIC_LOAD_TEMP]] to float
//CHECK: store float %[[LOAD]], ptr %[[Z]], align 4
omp.atomic.read %1 = %7 : !llvm.ptr, !llvm.ptr, i32

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = load atomic i64, ptr %[[Y]] monotonic, align 4
//CHECK: %[[CAST:.*]] = bitcast i64 %[[ATOMIC_LOAD_TEMP]] to double
//CHECK: %[[LOAD:.*]] = fptosi double %[[CAST]] to i32
//CHECK: store i32 %[[LOAD]], ptr %[[W]], align 4
omp.atomic.read %7 = %3 : !llvm.ptr, !llvm.ptr, f64

//CHECK: %[[ATOMIC_LOAD_TEMP:.*]] = load atomic i32, ptr %[[Z]] monotonic, align 4
//CHECK: %[[CAST:.*]] = bitcast i32 %[[ATOMIC_LOAD_TEMP]] to float
//CHECK: %[[LOAD:.*]] = fptosi float %[[CAST]] to i32
//CHECK: store i32 %[[LOAD]], ptr %[[W]], align 4
omp.atomic.read %7 = %1 : !llvm.ptr, !llvm.ptr, f32
llvm.return
}

// -----

// CHECK-LABEL: @omp_atomic_write
// CHECK-SAME: (ptr %[[x:.*]], i32 %[[expr:.*]])
llvm.func @omp_atomic_write(%x: !llvm.ptr, %expr: i32) -> () {
Expand Down
Loading