Skip to content

Commit 6145e20

Browse files
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.6 [skip ci]
2 parents cec7f8a + 653ed06 commit 6145e20

File tree

11 files changed

+193
-191
lines changed

11 files changed

+193
-191
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ Improvements to Clang's diagnostics
283283
pointers under ``-Wthread-safety-beta`` (still experimental), which reduces
284284
both false positives but also false negatives through more precise analysis.
285285

286+
- Clang now looks through parenthesis for ``-Wundefined-reinterpret-cast`` diagnostic.
287+
286288
Improvements to Clang's time-trace
287289
----------------------------------
288290

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14784,7 +14784,7 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK,
1478414784
QualType OpTy = Op->getType();
1478514785
QualType Result;
1478614786

14787-
if (isa<CXXReinterpretCastExpr>(Op)) {
14787+
if (isa<CXXReinterpretCastExpr>(Op->IgnoreParens())) {
1478814788
QualType OpOrigType = Op->IgnoreParenCasts()->getType();
1478914789
S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true,
1479014790
Op->getSourceRange());

clang/test/SemaCXX/reinterpret-cast.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ void dereference_reinterpret_cast() {
167167
(void)reinterpret_cast<float&>(d); // expected-warning {{reinterpret_cast from 'double' to 'float &' has undefined behavior}}
168168
(void)*reinterpret_cast<float*>(&d); // expected-warning {{dereference of type 'float *' that was reinterpret_cast from type 'double *' has undefined behavior}}
169169

170+
// Look through parens
171+
(void)*(reinterpret_cast<double*>(&l)); // expected-warning {{dereference of type 'double *' that was reinterpret_cast from type 'long *' has undefined behavior}}
172+
(void)*((reinterpret_cast<double*>((&l)))); // expected-warning {{dereference of type 'double *' that was reinterpret_cast from type 'long *' has undefined behavior}}
173+
170174
// TODO: add warning for tag types
171175
(void)reinterpret_cast<A&>(b);
172176
(void)*reinterpret_cast<A*>(&b);

llvm/include/llvm/Analysis/LoopInfo.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ class LLVM_ABI Loop : public LoopBase<BasicBlock, Loop> {
5959
};
6060

6161
/// Return true if the specified value is loop invariant.
62-
bool isLoopInvariant(const Value *V, bool HasCoroSuspendInst = false) const;
62+
bool isLoopInvariant(const Value *V) const;
6363

6464
/// Return true if all the operands of the specified instruction are loop
6565
/// invariant.
66-
bool hasLoopInvariantOperands(const Instruction *I,
67-
bool HasCoroSuspendInst = false) const;
66+
bool hasLoopInvariantOperands(const Instruction *I) const;
6867

6968
/// If the given value is an instruction inside of the loop and it can be
7069
/// hoisted, do so to make it trivially loop-invariant.

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ LLVM_ABI bool hoistRegion(DomTreeNode *, AAResults *, LoopInfo *,
185185
TargetLibraryInfo *, Loop *, MemorySSAUpdater &,
186186
ScalarEvolution *, ICFLoopSafetyInfo *,
187187
SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *,
188-
bool, bool AllowSpeculation,
189-
bool HasCoroSuspendInst = false);
188+
bool, bool AllowSpeculation);
190189

191190
/// Return true if the induction variable \p IV in a Loop whose latch is
192191
/// \p LatchBlock would become dead if the exit test \p Cond were removed.

llvm/lib/Analysis/LoopInfo.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,14 @@ static cl::opt<bool, true>
5858
// Loop implementation
5959
//
6060

61-
bool Loop::isLoopInvariant(const Value *V, bool HasCoroSuspendInst) const {
62-
if (const Instruction *I = dyn_cast<Instruction>(V)) {
63-
// FIXME: this is semantically inconsistent. We're tracking a proper fix in
64-
// issue #149604.
65-
// If V is a pointer to stack object and L contains a coro.suspend function
66-
// call, then V may not be loop invariant because the ramp function and
67-
// resume function have different stack frames.
68-
if (HasCoroSuspendInst && isa<AllocaInst>(I))
69-
return false;
70-
else
71-
return !contains(I);
72-
}
61+
bool Loop::isLoopInvariant(const Value *V) const {
62+
if (const Instruction *I = dyn_cast<Instruction>(V))
63+
return !contains(I);
7364
return true; // All non-instructions are loop invariant
7465
}
7566

76-
bool Loop::hasLoopInvariantOperands(const Instruction *I,
77-
bool HasCoroSuspendInst) const {
78-
return all_of(I->operands(), [&](Value *V) {
79-
return isLoopInvariant(V, HasCoroSuspendInst);
80-
});
67+
bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
68+
return all_of(I->operands(), [&](Value *V) { return isLoopInvariant(V); });
8169
}
8270

8371
bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI,
474474
if (Preheader)
475475
Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, AC, TLI, L,
476476
MSSAU, SE, &SafetyInfo, Flags, ORE, LoopNestMode,
477-
LicmAllowSpeculation, HasCoroSuspendInst);
477+
LicmAllowSpeculation);
478478

479479
// Now that all loop invariants have been removed from the loop, promote any
480480
// memory references to scalars that we can.
@@ -892,7 +892,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
892892
ICFLoopSafetyInfo *SafetyInfo,
893893
SinkAndHoistLICMFlags &Flags,
894894
OptimizationRemarkEmitter *ORE, bool LoopNestMode,
895-
bool AllowSpeculation, bool HasCoroSuspendInst) {
895+
bool AllowSpeculation) {
896896
// Verify inputs.
897897
assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&
898898
CurLoop != nullptr && SafetyInfo != nullptr &&
@@ -925,7 +925,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
925925
// TODO: It may be safe to hoist if we are hoisting to a conditional block
926926
// and we have accurately duplicated the control flow from the loop header
927927
// to that block.
928-
if (CurLoop->hasLoopInvariantOperands(&I, HasCoroSuspendInst) &&
928+
if (CurLoop->hasLoopInvariantOperands(&I) &&
929929
canSinkOrHoistInst(I, AA, DT, CurLoop, MSSAU, true, Flags, ORE) &&
930930
isSafeToExecuteUnconditionally(I, DT, TLI, CurLoop, SafetyInfo, ORE,
931931
Preheader->getTerminator(), AC,
@@ -975,7 +975,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
975975
SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop);
976976
};
977977
if ((IsInvariantStart(I) || isGuard(&I)) &&
978-
CurLoop->hasLoopInvariantOperands(&I, HasCoroSuspendInst) &&
978+
CurLoop->hasLoopInvariantOperands(&I) &&
979979
MustExecuteWithoutWritesBefore(I)) {
980980
hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo,
981981
MSSAU, SE, ORE);

llvm/test/Transforms/LICM/licm-coroutine.ll

Lines changed: 0 additions & 78 deletions
This file was deleted.

mlir/lib/Conversion/VectorToXeGPU/VectorToXeGPU.cpp

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,31 @@ static void adjustStridesForPermutation(AffineMap permMap,
180180
strides = applyPermutation(strides, perms64);
181181
}
182182

183-
// Computes memory strides for vector transfer operations, handling both
184-
// static and dynamic memrefs while applying permutation transformations
185-
// for XeGPU lowering.
186-
static SmallVector<Value> computeStrides(VectorTransferOpInterface xferOp,
187-
PatternRewriter &rewriter) {
183+
// Computes memory strides and a memref offset for vector transfer operations,
184+
// handling both static and dynamic memrefs while applying permutation
185+
// transformations for XeGPU lowering.
186+
static std::pair<SmallVector<Value>, Value>
187+
computeMemrefMeta(VectorTransferOpInterface xferOp, PatternRewriter &rewriter) {
188188
SmallVector<Value> strides;
189189
Value baseMemref = xferOp.getBase();
190190
AffineMap permMap = xferOp.getPermutationMap();
191191
MemRefType memrefType = dyn_cast<MemRefType>(baseMemref.getType());
192192

193193
Location loc = xferOp.getLoc();
194+
Value offsetVal = nullptr;
194195
if (memrefType.hasStaticShape()) {
195196
int64_t offset;
196197
SmallVector<int64_t> intStrides;
197198
if (failed(memrefType.getStridesAndOffset(intStrides, offset)))
198-
return {};
199+
return {{}, offsetVal};
199200
// Wrap static strides as MLIR values
200201
for (int64_t s : intStrides)
201202
strides.push_back(arith::ConstantIndexOp::create(rewriter, loc, s));
202-
} else {
203+
if (!ShapedType::isDynamic(offset))
204+
offsetVal = arith::ConstantIndexOp::create(rewriter, loc, offset);
205+
}
206+
207+
if (strides.empty() || !offsetVal) {
203208
// For dynamic shape memref, use memref.extract_strided_metadata to get
204209
// stride values
205210
unsigned rank = memrefType.getRank();
@@ -220,11 +225,16 @@ static SmallVector<Value> computeStrides(VectorTransferOpInterface xferOp,
220225

221226
auto meta = memref::ExtractStridedMetadataOp::create(
222227
rewriter, loc, resultTypes, baseMemref);
223-
strides.append(meta.getStrides().begin(), meta.getStrides().end());
228+
229+
if (strides.empty())
230+
strides.append(meta.getStrides().begin(), meta.getStrides().end());
231+
232+
if (!offsetVal)
233+
offsetVal = meta.getOffset();
224234
}
225235
// Adjust strides according to the permutation map (e.g., for transpose)
226236
adjustStridesForPermutation(permMap, strides);
227-
return strides;
237+
return {strides, offsetVal};
228238
}
229239

230240
// This function compute the vectors of localOffsets for scattered load/stores.
@@ -254,10 +264,10 @@ static SmallVector<Value> computeStrides(VectorTransferOpInterface xferOp,
254264
// %23 = arith.add %20, %21
255265
// %local_offsets = arith.add %22, %23
256266
// %orig_offset = %block_id_y * 4x2x6x32 // consider using affine map
257-
// %offsets = orig_offset + local_offsets
267+
// %offsets = memref_offset + orig_offset + local_offsets
258268
static Value computeOffsets(VectorTransferOpInterface xferOp,
259-
PatternRewriter &rewriter,
260-
ArrayRef<Value> strides) {
269+
PatternRewriter &rewriter, ArrayRef<Value> strides,
270+
Value baseOffset) {
261271
Location loc = xferOp.getLoc();
262272
VectorType vectorType = xferOp.getVectorType();
263273
SmallVector<Value> indices(xferOp.getIndices().begin(),
@@ -315,51 +325,30 @@ static Value computeOffsets(VectorTransferOpInterface xferOp,
315325
arith::AddIOp::create(rewriter, loc, localOffsets, broadcasted[i]);
316326

317327
// Compute base offset from transfer read indices
318-
Value baseOffset = nullptr;
319-
if (!indices.empty()) {
320-
baseOffset = arith::ConstantIndexOp::create(rewriter, loc, 0);
321-
for (size_t i = 0; i < indices.size(); ++i) {
322-
Value strideVal = strides[i];
323-
Value offsetContrib =
324-
arith::MulIOp::create(rewriter, loc, indices[i], strideVal);
325-
baseOffset =
326-
arith::AddIOp::create(rewriter, loc, baseOffset, offsetContrib);
327-
}
328-
// Broadcast base offset to match vector shape
329-
Value bcastBase = vector::BroadcastOp::create(
330-
rewriter, loc, fullIndexVectorType, baseOffset);
331-
localOffsets =
332-
arith::AddIOp::create(rewriter, loc, bcastBase, localOffsets);
328+
for (size_t i = 0; i < indices.size(); ++i) {
329+
Value strideVal = strides[i];
330+
Value offsetContrib =
331+
arith::MulIOp::create(rewriter, loc, indices[i], strideVal);
332+
baseOffset =
333+
arith::AddIOp::create(rewriter, loc, baseOffset, offsetContrib);
333334
}
335+
// Broadcast base offset to match vector shape
336+
Value bcastBase = vector::BroadcastOp::create(
337+
rewriter, loc, fullIndexVectorType, baseOffset);
338+
localOffsets = arith::AddIOp::create(rewriter, loc, bcastBase, localOffsets);
334339
return localOffsets;
335340
}
336341

337-
// Collapse memref shape to 1D
338-
static Value collapseMemrefTo1D(VectorTransferOpInterface xferOp,
339-
PatternRewriter &rewriter) {
342+
// Convert memref to i64 base pointer
343+
static Value memrefToIndexPtr(VectorTransferOpInterface xferOp,
344+
PatternRewriter &rewriter) {
340345
Location loc = xferOp.getLoc();
341-
342-
Value baseMemref = xferOp.getBase();
343-
MemRefType memrefType = dyn_cast<MemRefType>(baseMemref.getType());
344-
Type elementType = memrefType.getElementType();
345-
346-
// Compute the total number of elements in the memref
347-
MemRefType flatMemrefType;
348-
if (memrefType.hasStaticShape()) {
349-
auto totalElements = memrefType.getNumElements();
350-
flatMemrefType = MemRefType::get({totalElements}, elementType);
351-
} else {
352-
flatMemrefType = MemRefType::get({ShapedType::kDynamic}, elementType);
353-
}
354-
355-
SmallVector<ReassociationIndices> reassociation;
356-
ReassociationIndices allDims =
357-
llvm::to_vector(llvm::seq<int64_t>(0, memrefType.getRank()));
358-
reassociation.push_back(allDims);
359-
360-
auto collapseOp = memref::CollapseShapeOp::create(
361-
rewriter, loc, flatMemrefType, baseMemref, reassociation);
362-
return collapseOp;
346+
auto indexPtr = memref::ExtractAlignedPointerAsIndexOp::create(
347+
rewriter, loc, xferOp.getBase())
348+
.getResult();
349+
return arith::IndexCastOp::create(rewriter, loc, rewriter.getI64Type(),
350+
indexPtr)
351+
.getResult();
363352
}
364353

365354
static LogicalResult lowerToScatteredLoadOp(vector::TransferReadOp readOp,
@@ -372,13 +361,14 @@ static LogicalResult lowerToScatteredLoadOp(vector::TransferReadOp readOp,
372361
if (!memrefType)
373362
return rewriter.notifyMatchFailure(readOp, "Expected memref source");
374363

375-
SmallVector<Value> strides = computeStrides(readOp, rewriter);
376-
if (strides.empty())
364+
auto meta = computeMemrefMeta(readOp, rewriter);
365+
if (meta.first.empty())
377366
return rewriter.notifyMatchFailure(readOp, "Failed to compute strides");
378367

379-
Value localOffsets = computeOffsets(readOp, rewriter, strides);
368+
Value localOffsets =
369+
computeOffsets(readOp, rewriter, meta.first, meta.second);
380370

381-
Value flatMemref = collapseMemrefTo1D(readOp, rewriter);
371+
Value flatMemref = memrefToIndexPtr(readOp, rewriter);
382372

383373
Value mask = vector::ConstantMaskOp::create(
384374
rewriter, loc, VectorType::get(vectorShape, rewriter.getI1Type()),
@@ -405,11 +395,14 @@ static LogicalResult lowerToScatteredStoreOp(vector::TransferWriteOp writeOp,
405395
if (!memrefType)
406396
return rewriter.notifyMatchFailure(writeOp, "Expected memref source");
407397

408-
SmallVector<Value> strides = computeStrides(writeOp, rewriter);
398+
auto meta = computeMemrefMeta(writeOp, rewriter);
399+
if (meta.first.empty())
400+
return rewriter.notifyMatchFailure(writeOp, "Failed to compute strides");
409401

410-
Value localOffsets = computeOffsets(writeOp, rewriter, strides);
402+
Value localOffsets =
403+
computeOffsets(writeOp, rewriter, meta.first, meta.second);
411404

412-
Value flatMemref = collapseMemrefTo1D(writeOp, rewriter);
405+
Value flatMemref = memrefToIndexPtr(writeOp, rewriter);
413406

414407
Value mask = vector::ConstantMaskOp::create(
415408
rewriter, loc, VectorType::get(vectorShape, rewriter.getI1Type()),

0 commit comments

Comments
 (0)