Skip to content

Commit b5ba7d3

Browse files
Merge branch 'release/20.x' into bitcast-fix-llvm20
2 parents 9d7369b + 63e63f3 commit b5ba7d3

File tree

6 files changed

+97
-24
lines changed

6 files changed

+97
-24
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ Bug Fixes to C++ Support
10581058
- Fixed a substitution bug in transforming CTAD aliases when the type alias contains a non-pack template argument
10591059
corresponding to a pack parameter (#GH124715)
10601060
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
1061+
- Fixed an integer overflow bug in computing template parameter depths when synthesizing CTAD guides. (#GH128691)
10611062

10621063
Bug Fixes to AST Handling
10631064
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplateDeductionGuide.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,15 @@ struct ConvertConstructorToDeductionGuideTransform {
377377
if (NestedPattern)
378378
Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
379379
auto [Depth, Index] = getDepthAndIndex(Param);
380+
// Depth can still be 0 if FTD belongs to an explicit class template
381+
// specialization with an empty template parameter list. In that case,
382+
// we don't want the NewDepth to overflow, and it should remain 0.
383+
assert(Depth ||
384+
cast<ClassTemplateSpecializationDecl>(FTD->getDeclContext())
385+
->isExplicitSpecialization());
380386
NamedDecl *NewParam = transformTemplateParameter(
381-
SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment, Depth - 1);
387+
SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment,
388+
Depth ? Depth - 1 : 0);
382389
if (!NewParam)
383390
return nullptr;
384391
// Constraints require that we substitute depth-1 arguments

clang/test/SemaTemplate/deduction-guide.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,3 +691,35 @@ Test test(42);
691691
// CHECK-NEXT: | `-ParmVarDecl {{.*}} 'auto:1'
692692

693693
} // namespace GH122134
694+
695+
namespace GH128691 {
696+
697+
template <typename = void>
698+
class NewDeleteAllocator;
699+
700+
template <>
701+
struct NewDeleteAllocator<> {
702+
template <typename T>
703+
NewDeleteAllocator(T); // expected-note {{candidate template ignored}} \
704+
// expected-note {{implicit deduction guide declared as}}
705+
};
706+
707+
template <typename>
708+
struct NewDeleteAllocator : NewDeleteAllocator<> { // expected-note {{candidate template ignored}} \
709+
// expected-note {{implicit deduction guide declared as}}
710+
using NewDeleteAllocator<>::NewDeleteAllocator;
711+
};
712+
713+
void test() { NewDeleteAllocator abc(42); } // expected-error {{no viable constructor or deduction guide}}
714+
715+
// CHECK-LABEL: Dumping GH128691::<deduction guide for NewDeleteAllocator>:
716+
// CHECK-NEXT: FunctionTemplateDecl {{.+}} <deduction guide for NewDeleteAllocator>
717+
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 0
718+
// CHECK-NEXT: | `-TemplateArgument type 'void'
719+
// CHECK-NEXT: | |-inherited from TemplateTypeParm {{.+}} depth 0 index 0
720+
// CHECK-NEXT: | `-BuiltinType {{.+}} 'void'
721+
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 1 T
722+
// CHECK-NEXT: `-CXXDeductionGuideDecl {{.+}} <deduction guide for NewDeleteAllocator> 'auto (T) -> NewDeleteAllocator<type-parameter-0-0>'
723+
// CHECK-NEXT: `-ParmVarDecl {{.+}} 'T'
724+
725+
} // namespace GH128691

llvm/lib/Target/SystemZ/SystemZISelLowering.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10231,6 +10231,11 @@ static void printFunctionArgExts(const Function *F, raw_fd_ostream &OS) {
1023110231
void SystemZTargetLowering::
1023210232
verifyNarrowIntegerArgs_Call(const SmallVectorImpl<ISD::OutputArg> &Outs,
1023310233
const Function *F, SDValue Callee) const {
10234+
// Temporarily only do the check when explicitly requested, until it can be
10235+
// enabled by default.
10236+
if (!EnableIntArgExtCheck)
10237+
return;
10238+
1023410239
bool IsInternal = false;
1023510240
const Function *CalleeFn = nullptr;
1023610241
if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee))
@@ -10252,6 +10257,11 @@ verifyNarrowIntegerArgs_Call(const SmallVectorImpl<ISD::OutputArg> &Outs,
1025210257
void SystemZTargetLowering::
1025310258
verifyNarrowIntegerArgs_Ret(const SmallVectorImpl<ISD::OutputArg> &Outs,
1025410259
const Function *F) const {
10260+
// Temporarily only do the check when explicitly requested, until it can be
10261+
// enabled by default.
10262+
if (!EnableIntArgExtCheck)
10263+
return;
10264+
1025510265
if (!verifyNarrowIntegerArgs(Outs, isFullyInternal(F))) {
1025610266
errs() << "ERROR: Missing extension attribute of returned "
1025710267
<< "value from function:\n";
@@ -10268,11 +10278,6 @@ verifyNarrowIntegerArgs(const SmallVectorImpl<ISD::OutputArg> &Outs,
1026810278
if (IsInternal || !Subtarget.isTargetELF())
1026910279
return true;
1027010280

10271-
// Temporarily only do the check when explicitly requested, until it can be
10272-
// enabled by default.
10273-
if (!EnableIntArgExtCheck)
10274-
return true;
10275-
1027610281
if (EnableIntArgExtCheck.getNumOccurrences()) {
1027710282
if (!EnableIntArgExtCheck)
1027810283
return true;

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -638,17 +638,19 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
638638
(EnableMemCpyOptWithoutLibcalls ||
639639
(TLI->has(LibFunc_memcpy) && TLI->has(LibFunc_memmove)))) {
640640
MemoryLocation LoadLoc = MemoryLocation::get(LI);
641-
MemoryUseOrDef *LoadAccess = MSSA->getMemoryAccess(LI),
642-
*StoreAccess = MSSA->getMemoryAccess(SI);
643-
644-
// We use MSSA to check if an instruction may store to the memory we load
645-
// from in between the load and the store. If such an instruction is found,
646-
// we try to promote there instead of at the store position.
647-
auto *Clobber = MSSA->getWalker()->getClobberingMemoryAccess(
648-
StoreAccess->getDefiningAccess(), LoadLoc, BAA);
649-
Instruction *P = MSSA->dominates(LoadAccess, Clobber)
650-
? cast<MemoryUseOrDef>(Clobber)->getMemoryInst()
651-
: SI;
641+
642+
// We use alias analysis to check if an instruction may store to
643+
// the memory we load from in between the load and the store. If
644+
// such an instruction is found, we try to promote there instead
645+
// of at the store position.
646+
// TODO: Can use MSSA for this.
647+
Instruction *P = SI;
648+
for (auto &I : make_range(++LI->getIterator(), SI->getIterator())) {
649+
if (isModSet(BAA.getModRefInfo(&I, LoadLoc))) {
650+
P = &I;
651+
break;
652+
}
653+
}
652654

653655
// If we found an instruction that may write to the loaded memory,
654656
// we can try to promote at this position instead of the store

llvm/test/Transforms/MemCpyOpt/fca2memcpy.ll

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ define void @destroysrc(ptr %src, ptr %dst) {
5151

5252
define void @destroynoaliassrc(ptr noalias %src, ptr %dst) {
5353
; CHECK-LABEL: @destroynoaliassrc(
54-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST:%.*]], ptr align 8 [[SRC]], i64 16, i1 false)
55-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC:%.*]], i8 0, i64 16, i1 false)
54+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST:%.*]], ptr align 8 [[SRC:%.*]], i64 16, i1 false)
55+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC]], i8 0, i64 16, i1 false)
5656
; CHECK-NEXT: ret void
5757
;
5858
%1 = load %S, ptr %src
@@ -79,9 +79,9 @@ define void @copyalias(ptr %src, ptr %dst) {
7979
; sure we lift the computation as well if needed and possible.
8080
define void @addrproducer(ptr %src, ptr %dst) {
8181
; CHECK-LABEL: @addrproducer(
82-
; CHECK-NEXT: [[DST2:%.*]] = getelementptr [[S:%.*]], ptr [[DST]], i64 1
82+
; CHECK-NEXT: [[DST2:%.*]] = getelementptr [[S:%.*]], ptr [[DST:%.*]], i64 1
8383
; CHECK-NEXT: call void @llvm.memmove.p0.p0.i64(ptr align 8 [[DST2]], ptr align 8 [[SRC:%.*]], i64 16, i1 false)
84-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[DST:%.*]], i8 undef, i64 16, i1 false)
84+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[DST]], i8 undef, i64 16, i1 false)
8585
; CHECK-NEXT: ret void
8686
;
8787
%1 = load %S, ptr %src
@@ -113,8 +113,8 @@ define void @noaliasaddrproducer(ptr %src, ptr noalias %dst, ptr noalias %dstidp
113113
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[DSTIDPTR:%.*]], align 4
114114
; CHECK-NEXT: [[DSTINDEX:%.*]] = or i32 [[TMP2]], 1
115115
; CHECK-NEXT: [[DST2:%.*]] = getelementptr [[S:%.*]], ptr [[DST:%.*]], i32 [[DSTINDEX]]
116-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST2]], ptr align 8 [[SRC]], i64 16, i1 false)
117-
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC:%.*]], i8 undef, i64 16, i1 false)
116+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST2]], ptr align 8 [[SRC:%.*]], i64 16, i1 false)
117+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC]], i8 undef, i64 16, i1 false)
118118
; CHECK-NEXT: ret void
119119
;
120120
%1 = load %S, ptr %src
@@ -130,7 +130,7 @@ define void @throwing_call(ptr noalias %src, ptr %dst) {
130130
; CHECK-LABEL: @throwing_call(
131131
; CHECK-NEXT: [[TMP1:%.*]] = load [[S:%.*]], ptr [[SRC:%.*]], align 8
132132
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[SRC]], i8 0, i64 16, i1 false)
133-
; CHECK-NEXT: call void @call() [[ATTR2:#.*]]
133+
; CHECK-NEXT: call void @call() #[[ATTR2:[0-9]+]]
134134
; CHECK-NEXT: store [[S]] [[TMP1]], ptr [[DST:%.*]], align 8
135135
; CHECK-NEXT: ret void
136136
;
@@ -156,4 +156,30 @@ loop:
156156
br label %loop
157157
}
158158

159+
; There are multiple instructions that can clobber the source memory here.
160+
; We can move the dest write past the store to %ptr.24, but not the memcpy.
161+
; Make sure we don't perform fca2memcpy conversion in this case.
162+
define void @multiple_clobbering(ptr %ptr, ptr %ptr.copy) {
163+
; CHECK-LABEL: @multiple_clobbering(
164+
; CHECK-NEXT: [[PTR_8:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR:%.*]], i64 8
165+
; CHECK-NEXT: [[PTR_24:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 24
166+
; CHECK-NEXT: [[PTR_32:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 32
167+
; CHECK-NEXT: [[PTR_COPY_8:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR_COPY:%.*]], i64 8
168+
; CHECK-NEXT: [[STRUCT:%.*]] = load { i32, i64 }, ptr [[PTR_COPY_8]], align 8
169+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[PTR_8]], ptr [[PTR_32]], i64 12, i1 false)
170+
; CHECK-NEXT: store i64 1, ptr [[PTR_24]], align 8
171+
; CHECK-NEXT: store { i32, i64 } [[STRUCT]], ptr [[PTR_32]], align 8
172+
; CHECK-NEXT: ret void
173+
;
174+
%ptr.8 = getelementptr inbounds nuw i8, ptr %ptr, i64 8
175+
%ptr.24 = getelementptr inbounds nuw i8, ptr %ptr, i64 24
176+
%ptr.32 = getelementptr inbounds nuw i8, ptr %ptr, i64 32
177+
%ptr.copy.8 = getelementptr inbounds nuw i8, ptr %ptr.copy, i64 8
178+
%struct = load { i32, i64 }, ptr %ptr.copy.8, align 8
179+
call void @llvm.memcpy.p0.p0.i64(ptr %ptr.8, ptr %ptr.32, i64 12, i1 false)
180+
store i64 1, ptr %ptr.24, align 8
181+
store { i32, i64 } %struct, ptr %ptr.32, align 8
182+
ret void
183+
}
184+
159185
declare void @call()

0 commit comments

Comments
 (0)