Skip to content

Commit 0d66f01

Browse files
committed
[ValueTracking] Add CharWidth argument to getConstantStringInfo (NFC)
The method assumes that host chars and target chars have the same width. Add a CharWidth argument so that it can bail out if the requested char width differs from the host char width. Alternatively, the check could be done at call sites, but this is more error-prone. In the future, this method will be replaced with a different one that allows host/target chars to have different widths. The prototype will be the same except that StringRef is replaced with something that is byte width agnostic. Adding CharWidth argument now reduces the future diff.
1 parent 856e1f0 commit 0d66f01

File tree

10 files changed

+96
-46
lines changed

10 files changed

+96
-46
lines changed

clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void CodeGenFunction::ProcessOrderScopeAMDGCN(Value *Order, Value *Scope,
306306

307307
// Some of the atomic builtins take the scope as a string name.
308308
StringRef scp;
309-
if (llvm::getConstantStringInfo(Scope, scp)) {
309+
if (llvm::getConstantStringInfo(Scope, scp, /*CharWidth=*/8)) {
310310
if (getTarget().getTriple().isSPIRV())
311311
scp = mapScopeToSPIRV(scp);
312312
SSID = getLLVMContext().getOrInsertSyncScopeID(scp);
@@ -369,7 +369,7 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
369369
for (unsigned K = 2; K < E->getNumArgs(); ++K) {
370370
llvm::Value *V = EmitScalarExpr(E->getArg(K));
371371
StringRef AS;
372-
if (llvm::getConstantStringInfo(V, AS)) {
372+
if (llvm::getConstantStringInfo(V, AS, /*CharWidth=*/8)) {
373373
MMRAs.push_back({Tag, AS});
374374
// TODO: Delete the resulting unused constant?
375375
continue;

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ LLVM_ABI bool getConstantDataArrayInfo(const Value *V,
399399
/// trailing null characters as well as any other characters that come after
400400
/// it.
401401
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str,
402-
bool TrimAtNul = true);
402+
unsigned CharWidth, bool TrimAtNul = true);
403403

404404
/// If we can compute the length of the string pointed to by the specified
405405
/// pointer, return 'len+1'. If we can't, return 0.

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6628,9 +6628,12 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
66286628
/// return true. When TrimAtNul is set, Str will contain only the bytes up
66296629
/// to but not including the first nul. Return false on failure.
66306630
bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
6631-
bool TrimAtNul) {
6631+
unsigned CharWidth, bool TrimAtNul) {
6632+
if (CharWidth != CHAR_BIT)
6633+
return false;
6634+
66326635
ConstantDataArraySlice Slice;
6633-
if (!getConstantDataArrayInfo(V, Slice, 8))
6636+
if (!getConstantDataArrayInfo(V, Slice, CharWidth))
66346637
return false;
66356638

66366639
if (Slice.Array == nullptr) {

llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static_assert(NonLiteralStr.size() == 3);
121121

122122
static StringRef getAsConstantStr(Value *V) {
123123
StringRef S;
124-
if (!getConstantStringInfo(V, S))
124+
if (!getConstantStringInfo(V, S, /*CharWidth=*/8))
125125
S = NonLiteralStr;
126126

127127
return S;
@@ -154,7 +154,7 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
154154
Value *Op = CI->getArgOperand(0);
155155

156156
StringRef FormatStr;
157-
if (!getConstantStringInfo(Op, FormatStr)) {
157+
if (!getConstantStringInfo(Op, FormatStr, /*CharWidth=*/8)) {
158158
Value *Stripped = Op->stripPointerCasts();
159159
if (!isa<UndefValue>(Stripped) && !isa<ConstantPointerNull>(Stripped))
160160
diagnoseInvalidFormatString(CI);

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
707707

708708
StringRef AnnotationString;
709709
[[maybe_unused]] bool Success =
710-
getConstantStringInfo(GV, AnnotationString);
710+
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
711711
assert(Success && "Failed to get annotation string");
712712
MCInst Inst;
713713
Inst.setOpcode(SPIRV::OpDecorate);

llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static std::string getAnnotation(Value *AnnoVal, Value *OptAnnoVal) {
166166
std::string Anno;
167167
if (auto *C = dyn_cast_or_null<Constant>(AnnoVal)) {
168168
StringRef Str;
169-
if (getConstantStringInfo(C, Str))
169+
if (getConstantStringInfo(C, Str, /*CharWidth=*/8))
170170
Anno = Str;
171171
}
172172
// handle optional annotation parameter in a way that Khronos Translator do

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
572572
// The second field is a pointer to a global annotation string.
573573
auto *GV = cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
574574
StringRef AnnotationString;
575-
getConstantStringInfo(GV, AnnotationString);
575+
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
576576
auto *Sym = static_cast<MCSymbolWasm *>(getSymbol(F));
577577
CustomSections[AnnotationString].push_back(Sym);
578578
}

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,8 @@ class StrNCmpInliner {
11951195
/// handled by the instcombine pass.
11961196
///
11971197
bool StrNCmpInliner::optimizeStrNCmp() {
1198+
unsigned CharWidth = DL.getByteWidth();
1199+
11981200
if (StrNCmpInlineThreshold < 2)
11991201
return false;
12001202

@@ -1208,8 +1210,10 @@ bool StrNCmpInliner::optimizeStrNCmp() {
12081210
return false;
12091211

12101212
StringRef Str1, Str2;
1211-
bool HasStr1 = getConstantStringInfo(Str1P, Str1, /*TrimAtNul=*/false);
1212-
bool HasStr2 = getConstantStringInfo(Str2P, Str2, /*TrimAtNul=*/false);
1213+
bool HasStr1 =
1214+
getConstantStringInfo(Str1P, Str1, CharWidth, /*TrimAtNul=*/false);
1215+
bool HasStr2 =
1216+
getConstantStringInfo(Str2P, Str2, CharWidth, /*TrimAtNul=*/false);
12131217
if (HasStr1 == HasStr2)
12141218
return false;
12151219

@@ -1347,12 +1351,14 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
13471351
/// Convert memchr with a small constant string into a switch
13481352
static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
13491353
const DataLayout &DL) {
1354+
unsigned CharWidth = DL.getByteWidth();
1355+
13501356
if (isa<Constant>(Call->getArgOperand(1)))
13511357
return false;
13521358

13531359
StringRef Str;
13541360
Value *Base = Call->getArgOperand(0);
1355-
if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/false))
1361+
if (!getConstantStringInfo(Base, Str, CharWidth, /*TrimAtNul=*/false))
13561362
return false;
13571363

13581364
uint64_t N = Str.size();

llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static Value *callBufferedPrintfStart(
250250
for (size_t i = 1; i < Args.size(); i++) {
251251
if (SpecIsCString.test(i)) {
252252
StringRef ArgStr;
253-
if (getConstantStringInfo(Args[i], ArgStr)) {
253+
if (getConstantStringInfo(Args[i], ArgStr, /*CharWidth=*/8)) {
254254
auto alignedLen = alignTo(ArgStr.size() + 1, 8);
255255
StringContents.push_back(StringData(
256256
ArgStr,
@@ -432,7 +432,7 @@ Value *llvm::emitAMDGPUPrintfCall(IRBuilder<> &Builder, ArrayRef<Value *> Args,
432432
SparseBitVector<8> SpecIsCString;
433433
StringRef FmtStr;
434434

435-
if (getConstantStringInfo(Fmt, FmtStr))
435+
if (getConstantStringInfo(Fmt, FmtStr, /*CharWidth=*/8))
436436
locateCStrings(SpecIsCString, FmtStr);
437437

438438
if (IsBuffered) {

0 commit comments

Comments
 (0)