Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void CodeGenFunction::ProcessOrderScopeAMDGCN(Value *Order, Value *Scope,

// Some of the atomic builtins take the scope as a string name.
StringRef scp;
if (llvm::getConstantStringInfo(Scope, scp)) {
if (llvm::getConstantStringInfo(Scope, scp, /*CharWidth=*/8)) {
if (getTarget().getTriple().isSPIRV())
scp = mapScopeToSPIRV(scp);
SSID = getLLVMContext().getOrInsertSyncScopeID(scp);
Expand Down Expand Up @@ -369,7 +369,7 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
for (unsigned K = 2; K < E->getNumArgs(); ++K) {
llvm::Value *V = EmitScalarExpr(E->getArg(K));
StringRef AS;
if (llvm::getConstantStringInfo(V, AS)) {
if (llvm::getConstantStringInfo(V, AS, /*CharWidth=*/8)) {
MMRAs.push_back({Tag, AS});
// TODO: Delete the resulting unused constant?
continue;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/ValueTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ LLVM_ABI bool getConstantDataArrayInfo(const Value *V,
/// trailing null characters as well as any other characters that come after
/// it.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str,
bool TrimAtNul = true);
unsigned CharWidth, bool TrimAtNul = true);

/// If we can compute the length of the string pointed to by the specified
/// pointer, return 'len+1'. If we can't, return 0.
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6628,9 +6628,12 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
/// return true. When TrimAtNul is set, Str will contain only the bytes up
/// to but not including the first nul. Return false on failure.
bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
bool TrimAtNul) {
unsigned CharWidth, bool TrimAtNul) {
if (CharWidth != CHAR_BIT)
return false;

ConstantDataArraySlice Slice;
if (!getConstantDataArrayInfo(V, Slice, 8))
if (!getConstantDataArrayInfo(V, Slice, CharWidth))
return false;

if (Slice.Array == nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static_assert(NonLiteralStr.size() == 3);

static StringRef getAsConstantStr(Value *V) {
StringRef S;
if (!getConstantStringInfo(V, S))
if (!getConstantStringInfo(V, S, /*CharWidth=*/8))
S = NonLiteralStr;

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

StringRef FormatStr;
if (!getConstantStringInfo(Op, FormatStr)) {
if (!getConstantStringInfo(Op, FormatStr, /*CharWidth=*/8)) {
Value *Stripped = Op->stripPointerCasts();
if (!isa<UndefValue>(Stripped) && !isa<ConstantPointerNull>(Stripped))
diagnoseInvalidFormatString(CI);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {

StringRef AnnotationString;
[[maybe_unused]] bool Success =
getConstantStringInfo(GV, AnnotationString);
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
assert(Success && "Failed to get annotation string");
MCInst Inst;
Inst.setOpcode(SPIRV::OpDecorate);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static std::string getAnnotation(Value *AnnoVal, Value *OptAnnoVal) {
std::string Anno;
if (auto *C = dyn_cast_or_null<Constant>(AnnoVal)) {
StringRef Str;
if (getConstantStringInfo(C, Str))
if (getConstantStringInfo(C, Str, /*CharWidth=*/8))
Anno = Str;
}
// handle optional annotation parameter in a way that Khronos Translator do
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
// The second field is a pointer to a global annotation string.
auto *GV = cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
StringRef AnnotationString;
getConstantStringInfo(GV, AnnotationString);
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
auto *Sym = static_cast<MCSymbolWasm *>(getSymbol(F));
CustomSections[AnnotationString].push_back(Sym);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,8 @@ class StrNCmpInliner {
/// handled by the instcombine pass.
///
bool StrNCmpInliner::optimizeStrNCmp() {
unsigned CharWidth = DL.getByteWidth();

if (StrNCmpInlineThreshold < 2)
return false;

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

StringRef Str1, Str2;
bool HasStr1 = getConstantStringInfo(Str1P, Str1, /*TrimAtNul=*/false);
bool HasStr2 = getConstantStringInfo(Str2P, Str2, /*TrimAtNul=*/false);
bool HasStr1 =
getConstantStringInfo(Str1P, Str1, CharWidth, /*TrimAtNul=*/false);
bool HasStr2 =
getConstantStringInfo(Str2P, Str2, CharWidth, /*TrimAtNul=*/false);
if (HasStr1 == HasStr2)
return false;

Expand Down Expand Up @@ -1347,12 +1351,14 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
/// Convert memchr with a small constant string into a switch
static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
const DataLayout &DL) {
unsigned CharWidth = DL.getByteWidth();

if (isa<Constant>(Call->getArgOperand(1)))
return false;

StringRef Str;
Value *Base = Call->getArgOperand(0);
if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/false))
if (!getConstantStringInfo(Base, Str, CharWidth, /*TrimAtNul=*/false))
return false;

uint64_t N = Str.size();
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static Value *callBufferedPrintfStart(
for (size_t i = 1; i < Args.size(); i++) {
if (SpecIsCString.test(i)) {
StringRef ArgStr;
if (getConstantStringInfo(Args[i], ArgStr)) {
if (getConstantStringInfo(Args[i], ArgStr, /*CharWidth=*/8)) {
auto alignedLen = alignTo(ArgStr.size() + 1, 8);
StringContents.push_back(StringData(
ArgStr,
Expand Down Expand Up @@ -432,7 +432,7 @@ Value *llvm::emitAMDGPUPrintfCall(IRBuilder<> &Builder, ArrayRef<Value *> Args,
SparseBitVector<8> SpecIsCString;
StringRef FmtStr;

if (getConstantStringInfo(Fmt, FmtStr))
if (getConstantStringInfo(Fmt, FmtStr, /*CharWidth=*/8))
locateCStrings(SpecIsCString, FmtStr);

if (IsBuffered) {
Expand Down
Loading
Loading