Skip to content

Commit 09b0498

Browse files
belitskiycopybara-github
authored andcommitted
Patch out an ambiguous reference to memcpy in LLVM.
This fixes the Windows presubmit. PiperOrigin-RevId: 694499615
1 parent a79df1a commit 09b0498

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed
+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
2+
index 459b4d2f6227..3130a0bd2955 100644
3+
--- a/llvm/lib/IR/Intrinsics.cpp
4+
+++ b/llvm/lib/IR/Intrinsics.cpp
5+
@@ -32,7 +32,6 @@
6+
#include "llvm/IR/Type.h"
7+
8+
using namespace llvm;
9+
-using namespace Intrinsic;
10+
11+
/// Table of string intrinsic names indexed by enum value.
12+
static constexpr const char *const IntrinsicNameTable[] = {
13+
@@ -49,7 +48,7 @@ StringRef Intrinsic::getBaseName(ID id) {
14+
15+
StringRef Intrinsic::getName(ID id) {
16+
assert(id < num_intrinsics && "Invalid intrinsic ID!");
17+
- assert(!isOverloaded(id) &&
18+
+ assert(!Intrinsic::isOverloaded(id) &&
19+
"This version of getName does not support overloading");
20+
return getBaseName(id);
21+
}
22+
@@ -152,27 +151,27 @@ static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) {
23+
return Result;
24+
}
25+
26+
-static std::string getIntrinsicNameImpl(ID Id, ArrayRef<Type *> Tys, Module *M,
27+
- FunctionType *FT,
28+
+static std::string getIntrinsicNameImpl(Intrinsic::ID Id, ArrayRef<Type *> Tys,
29+
+ Module *M, FunctionType *FT,
30+
bool EarlyModuleCheck) {
31+
32+
- assert(Id < num_intrinsics && "Invalid intrinsic ID!");
33+
- assert((Tys.empty() || isOverloaded(Id)) &&
34+
+ assert(Id < Intrinsic::num_intrinsics && "Invalid intrinsic ID!");
35+
+ assert((Tys.empty() || Intrinsic::isOverloaded(Id)) &&
36+
"This version of getName is for overloaded intrinsics only");
37+
(void)EarlyModuleCheck;
38+
assert((!EarlyModuleCheck || M ||
39+
!any_of(Tys, [](Type *T) { return isa<PointerType>(T); })) &&
40+
"Intrinsic overloading on pointer types need to provide a Module");
41+
bool HasUnnamedType = false;
42+
- std::string Result(getBaseName(Id));
43+
+ std::string Result(Intrinsic::getBaseName(Id));
44+
for (Type *Ty : Tys)
45+
Result += "." + getMangledTypeStr(Ty, HasUnnamedType);
46+
if (HasUnnamedType) {
47+
assert(M && "unnamed types need a module");
48+
if (!FT)
49+
- FT = getType(M->getContext(), Id, Tys);
50+
+ FT = Intrinsic::getType(M->getContext(), Id, Tys);
51+
else
52+
- assert((FT == getType(M->getContext(), Id, Tys)) &&
53+
+ assert((FT == Intrinsic::getType(M->getContext(), Id, Tys)) &&
54+
"Provided FunctionType must match arguments");
55+
return M->getUniqueIntrinsicName(Result, Id, FT);
56+
}
57+
@@ -199,10 +198,13 @@ enum IIT_Info {
58+
#undef GET_INTRINSIC_IITINFO
59+
};
60+
61+
-static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
62+
- IIT_Info LastInfo,
63+
- SmallVectorImpl<IITDescriptor> &OutputTable) {
64+
- bool IsScalableVector = LastInfo == IIT_SCALABLE_VEC;
65+
+static void
66+
+DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
67+
+ IIT_Info LastInfo,
68+
+ SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
69+
+ using namespace Intrinsic;
70+
+
71+
+ bool IsScalableVector = (LastInfo == IIT_SCALABLE_VEC);
72+
73+
IIT_Info Info = IIT_Info(Infos[NextElt++]);
74+
unsigned StructElts = 2;
75+
@@ -479,8 +481,10 @@ void Intrinsic::getIntrinsicInfoTableEntries(
76+
DecodeIITType(NextElt, IITEntries, IIT_Done, T);
77+
}
78+
79+
-static Type *DecodeFixedType(ArrayRef<IITDescriptor> &Infos,
80+
+static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
81+
ArrayRef<Type *> Tys, LLVMContext &Context) {
82+
+ using namespace Intrinsic;
83+
+
84+
IITDescriptor D = Infos.front();
85+
Infos = Infos.slice(1);
86+
87+
@@ -613,10 +617,13 @@ bool Intrinsic::isOverloaded(ID id) {
88+
#include "llvm/IR/IntrinsicImpl.inc"
89+
#undef GET_INTRINSIC_TARGET_DATA
90+
91+
-bool Intrinsic::isTargetIntrinsic(ID IID) { return IID > TargetInfos[0].Count; }
92+
+bool Intrinsic::isTargetIntrinsic(Intrinsic::ID IID) {
93+
+ return IID > TargetInfos[0].Count;
94+
+}
95+
96+
-int Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
97+
- StringRef Name, StringRef Target) {
98+
+int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
99+
+ StringRef Name,
100+
+ StringRef Target) {
101+
assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix");
102+
assert(Name.drop_front(5).starts_with(Target) && "Unexpected target");
103+
104+
@@ -678,23 +685,24 @@ findTargetSubtable(StringRef Name) {
105+
106+
/// This does the actual lookup of an intrinsic ID which matches the given
107+
/// function name.
108+
-ID Intrinsic::lookupIntrinsicID(StringRef Name) {
109+
+Intrinsic::ID Intrinsic::lookupIntrinsicID(StringRef Name) {
110+
auto [NameTable, Target] = findTargetSubtable(Name);
111+
- int Idx = lookupLLVMIntrinsicByName(NameTable, Name, Target);
112+
+ int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name, Target);
113+
if (Idx == -1)
114+
- return not_intrinsic;
115+
+ return Intrinsic::not_intrinsic;
116+
117+
// Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
118+
// an index into a sub-table.
119+
int Adjust = NameTable.data() - IntrinsicNameTable;
120+
- ID Id = static_cast<ID>(Idx + Adjust);
121+
+ Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
122+
123+
// If the intrinsic is not overloaded, require an exact match. If it is
124+
// overloaded, require either exact or prefix match.
125+
const auto MatchSize = strlen(NameTable[Idx]);
126+
assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
127+
bool IsExactMatch = Name.size() == MatchSize;
128+
- return IsExactMatch || isOverloaded(Id) ? Id : not_intrinsic;
129+
+ return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID
130+
+ : Intrinsic::not_intrinsic;
131+
}
132+
133+
/// This defines the "Intrinsic::getAttributes(ID id)" method.
134+
@@ -735,7 +743,8 @@ Function *Intrinsic::getDeclarationIfExists(Module *M, ID id,
135+
136+
bool Intrinsic::isConstrainedFPIntrinsic(ID QID) {
137+
switch (QID) {
138+
-#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) case INTRINSIC:
139+
+#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
140+
+ case Intrinsic::INTRINSIC:
141+
#include "llvm/IR/ConstrainedOps.def"
142+
#undef INSTRUCTION
143+
return true;
144+
@@ -744,10 +753,10 @@ bool Intrinsic::isConstrainedFPIntrinsic(ID QID) {
145+
}
146+
}
147+
148+
-bool Intrinsic::hasConstrainedFPRoundingModeOperand(ID QID) {
149+
+bool Intrinsic::hasConstrainedFPRoundingModeOperand(Intrinsic::ID QID) {
150+
switch (QID) {
151+
#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
152+
- case INTRINSIC: \
153+
+ case Intrinsic::INTRINSIC: \
154+
return ROUND_MODE == 1;
155+
#include "llvm/IR/ConstrainedOps.def"
156+
#undef INSTRUCTION
157+
@@ -756,13 +765,16 @@ bool Intrinsic::hasConstrainedFPRoundingModeOperand(ID QID) {
158+
}
159+
}
160+
161+
-using DeferredIntrinsicMatchPair = std::pair<Type *, ArrayRef<IITDescriptor>>;
162+
+using DeferredIntrinsicMatchPair =
163+
+ std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>;
164+
165+
static bool
166+
-matchIntrinsicType(Type *Ty, ArrayRef<IITDescriptor> &Infos,
167+
+matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
168+
SmallVectorImpl<Type *> &ArgTys,
169+
SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks,
170+
bool IsDeferredCheck) {
171+
+ using namespace Intrinsic;
172+
+
173+
// If we ran out of descriptors, there are too many arguments.
174+
if (Infos.empty())
175+
return true;
176+
@@ -981,9 +993,9 @@ matchIntrinsicType(Type *Ty, ArrayRef<IITDescriptor> &Infos,
177+
llvm_unreachable("unhandled");
178+
}
179+
180+
-MatchIntrinsicTypesResult
181+
+Intrinsic::MatchIntrinsicTypesResult
182+
Intrinsic::matchIntrinsicSignature(FunctionType *FTy,
183+
- ArrayRef<IITDescriptor> &Infos,
184+
+ ArrayRef<Intrinsic::IITDescriptor> &Infos,
185+
SmallVectorImpl<Type *> &ArgTys) {
186+
SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks;
187+
if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks,
188+
@@ -1007,8 +1019,8 @@ Intrinsic::matchIntrinsicSignature(FunctionType *FTy,
189+
return MatchIntrinsicTypes_Match;
190+
}
191+
192+
-bool Intrinsic::matchIntrinsicVarArg(bool isVarArg,
193+
- ArrayRef<IITDescriptor> &Infos) {
194+
+bool Intrinsic::matchIntrinsicVarArg(
195+
+ bool isVarArg, ArrayRef<Intrinsic::IITDescriptor> &Infos) {
196+
// If there are no descriptors left, then it can't be a vararg.
197+
if (Infos.empty())
198+
return isVarArg;
199+
@@ -1026,20 +1038,20 @@ bool Intrinsic::matchIntrinsicVarArg(bool isVarArg,
200+
return true;
201+
}
202+
203+
-bool Intrinsic::getIntrinsicSignature(ID ID, FunctionType *FT,
204+
+bool Intrinsic::getIntrinsicSignature(Intrinsic::ID ID, FunctionType *FT,
205+
SmallVectorImpl<Type *> &ArgTys) {
206+
if (!ID)
207+
return false;
208+
209+
- SmallVector<IITDescriptor, 8> Table;
210+
+ SmallVector<Intrinsic::IITDescriptor, 8> Table;
211+
getIntrinsicInfoTableEntries(ID, Table);
212+
- ArrayRef<IITDescriptor> TableRef = Table;
213+
+ ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
214+
215+
- if (matchIntrinsicSignature(FT, TableRef, ArgTys) !=
216+
- MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
217+
+ if (Intrinsic::matchIntrinsicSignature(FT, TableRef, ArgTys) !=
218+
+ Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
219+
return false;
220+
}
221+
- if (matchIntrinsicVarArg(FT->isVarArg(), TableRef))
222+
+ if (Intrinsic::matchIntrinsicVarArg(FT->isVarArg(), TableRef))
223+
return false;
224+
return true;
225+
}
226+
@@ -1055,10 +1067,10 @@ std::optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) {
227+
if (!getIntrinsicSignature(F, ArgTys))
228+
return std::nullopt;
229+
230+
- ID ID = F->getIntrinsicID();
231+
+ Intrinsic::ID ID = F->getIntrinsicID();
232+
StringRef Name = F->getName();
233+
std::string WantedName =
234+
- getName(ID, ArgTys, F->getParent(), F->getFunctionType());
235+
+ Intrinsic::getName(ID, ArgTys, F->getParent(), F->getFunctionType());
236+
if (Name == WantedName)
237+
return std::nullopt;
238+
239+
@@ -1074,7 +1086,7 @@ std::optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) {
240+
// invalid and we'll get an error.
241+
ExistingGV->setName(WantedName + ".renamed");
242+
}
243+
- return getOrInsertDeclaration(F->getParent(), ID, ArgTys);
244+
+ return Intrinsic::getOrInsertDeclaration(F->getParent(), ID, ArgTys);
245+
}();
246+
247+
NewDecl->setCallingConv(F->getCallingConv());

third_party/llvm/workspace.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def repo(name):
2323
"//third_party/llvm:toolchains.patch",
2424
"//third_party/llvm:zstd.patch",
2525
"//third_party/llvm:rocdl_shuffle_down.patch",
26+
"//third_party/llvm:intrinsic_memcpy.patch",
2627
],
2728
link_files = {"//third_party/llvm:run_lit.sh": "mlir/run_lit.sh"},
2829
)

0 commit comments

Comments
 (0)