-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think this can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually if you consider the test case:
Then we need to extract the real component of |
||
XRead = Builder.CreateFPToSI(XRead, VType); | ||
else if (VType->isFloatingPointTy() && XReadType->isIntegerTy()) | ||
XRead = Builder.CreateSIToFP(XRead, VType); | ||
Comment on lines
+284
to
+286
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
@@ -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); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).