Skip to content

Commit 154d10f

Browse files
authored
Merge pull request #38110 from etcwilde/ewilde/swift-next/fix-APInt-toString
[Swift next] Fixes for APInt::toString API changes
2 parents 7150705 + 9bd93db commit 154d10f

File tree

7 files changed

+46
-44
lines changed

7 files changed

+46
-44
lines changed

lib/Driver/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,7 @@ static void addDiagFileOutputForPersistentPCHAction(
27602760
llvm::sys::path::append(outPathBuf, stem);
27612761
outPathBuf += '-';
27622762
auto code = llvm::hash_value(ModuleOutPath);
2763-
outPathBuf += llvm::APInt(64, code).toString(36, /*Signed=*/false);
2763+
llvm::APInt(64, code).toString(outPathBuf, 36, /*Signed=*/false);
27642764
llvm::sys::path::replace_extension(outPathBuf, suffix);
27652765
}
27662766

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
#include "swift/SIL/SILModule.h"
3131
#include "swift/SILOptimizer/PassManager/Passes.h"
3232
#include "swift/SILOptimizer/Utils/Generics.h"
33+
#include "swift/Serialization/ModuleDependencyScanner.h"
3334
#include "swift/Serialization/SerializationOptions.h"
3435
#include "swift/Serialization/SerializedModuleLoader.h"
35-
#include "swift/Serialization/ModuleDependencyScanner.h"
3636
#include "swift/Strings.h"
3737
#include "swift/Subsystems.h"
3838
#include "clang/AST/ASTContext.h"
@@ -44,6 +44,7 @@
4444
#include "llvm/Support/MemoryBuffer.h"
4545
#include "llvm/Support/Path.h"
4646
#include "llvm/Support/Process.h"
47+
#include <llvm/ADT/StringExtras.h>
4748

4849
using namespace swift;
4950

@@ -61,7 +62,7 @@ std::string CompilerInvocation::getPCHHash() const {
6162
SILOpts.getPCHHashComponents(),
6263
IRGenOpts.getPCHHashComponents());
6364

64-
return llvm::APInt(64, Code).toString(36, /*Signed=*/false);
65+
return llvm::toString(llvm::APInt(64, Code), 36, /*Signed=*/false);
6566
}
6667

6768
const PrimarySpecificPaths &

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/ADT/APInt.h"
3434
#include "llvm/ADT/Hashing.h"
3535
#include "llvm/ADT/STLExtras.h"
36+
#include "llvm/ADT/StringExtras.h"
3637
#include "llvm/Support/CommandLine.h"
3738
#include "llvm/Support/Debug.h"
3839
#include "llvm/Support/Errc.h"
@@ -1554,7 +1555,7 @@ InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath) {
15541555
// ensure that we compile all swift interface files with the option set.
15551556
unsigned(genericSubInvocation.getSILOptions().EnableOSSAModules));
15561557

1557-
return llvm::APInt(64, H).toString(36, /*Signed=*/false);
1558+
return llvm::toString(llvm::APInt(64, H), 36, /*Signed=*/false);
15581559
}
15591560

15601561
std::error_code

lib/IRGen/GenIntegerLiteral.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
#include "GenIntegerLiteral.h"
1818

19+
#include "swift/ABI/MetadataValues.h"
20+
#include "llvm/ADT/StringExtras.h"
1921
#include "llvm/IR/Constants.h"
2022
#include "llvm/IR/GlobalVariable.h"
21-
#include "swift/ABI/MetadataValues.h"
2223

2324
#include "BitPatternBuilder.h"
2425
#include "Explosion.h"
@@ -177,12 +178,12 @@ ConstantIntegerLiteralMap::get(IRGenModule &IGM, APInt &&value) {
177178
// TODO: make this shared within the image
178179
auto arrayTy = llvm::ArrayType::get(IGM.SizeTy, numChunks);
179180
auto initV = llvm::ConstantArray::get(arrayTy, chunks);
180-
auto globalArray =
181-
new llvm::GlobalVariable(*IGM.getModule(), arrayTy, /*constant*/ true,
182-
llvm::GlobalVariable::PrivateLinkage, initV,
183-
IGM.EnableValueNames
184-
? Twine("intliteral.") + value.toString(10, true)
185-
: "");
181+
auto globalArray = new llvm::GlobalVariable(
182+
*IGM.getModule(), arrayTy, /*constant*/ true,
183+
llvm::GlobalVariable::PrivateLinkage, initV,
184+
IGM.EnableValueNames
185+
? Twine("intliteral.") + llvm::toString(value, 10, true)
186+
: "");
186187
globalArray->setUnnamedAddr(llvm::GlobalVariable::UnnamedAddr::Global);
187188

188189
// Various clients expect this to be a i64*, not an [N x i64]*, so cast down.

lib/SIL/IR/SILPrinter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,12 +1346,12 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
13461346
*this << ILI->getType() << ", " << lit;
13471347
}
13481348
void visitFloatLiteralInst(FloatLiteralInst *FLI) {
1349-
*this << FLI->getType() << ", 0x";
1350-
APInt bits = FLI->getBits();
1351-
*this << bits.toString(16, /*Signed*/ false);
1349+
llvm::SmallString<12> hex;
13521350
llvm::SmallString<12> decimal;
1351+
FLI->getBits().toString(hex, 16, /*Signed*/ false);
13531352
FLI->getValue().toString(decimal);
1354-
*this << " // " << decimal;
1353+
*this << FLI->getType()
1354+
<< (llvm::Twine(", 0x") + hex + " // " + decimal).str();
13551355
}
13561356
static StringRef getStringEncodingName(StringLiteralInst::Encoding kind) {
13571357
switch (kind) {

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/APFloat.h"
2424
#include "llvm/ADT/APSInt.h"
2525
#include "llvm/ADT/Statistic.h"
26+
#include "llvm/ADT/StringExtras.h"
2627
#include "llvm/IR/Intrinsics.h"
2728
#include "llvm/Support/Debug.h"
2829

@@ -245,24 +246,23 @@ constantFoldBinaryWithOverflow(BuiltinInst *BI, llvm::Intrinsic::ID ID,
245246
break;
246247
}
247248

249+
SmallString<10> LhsStr;
250+
SmallString<10> RhsStr;
251+
LHSInt.toString(LhsStr, /*Radix*/ 10, Signed);
252+
RHSInt.toString(RhsStr, /*Radix*/ 10, Signed);
248253
if (!OpType.isNull()) {
249-
diagnose(BI->getModule().getASTContext(),
250-
Loc.getSourceLoc(),
251-
diag::arithmetic_operation_overflow,
252-
LHSInt.toString(/*Radix*/ 10, Signed),
253-
Operator,
254-
RHSInt.toString(/*Radix*/ 10, Signed),
255-
OpType).highlight(LHSRange).highlight(RHSRange);
254+
diagnose(BI->getModule().getASTContext(), Loc.getSourceLoc(),
255+
diag::arithmetic_operation_overflow, LhsStr, Operator, RhsStr,
256+
OpType)
257+
.highlight(LHSRange)
258+
.highlight(RHSRange);
256259
} else {
257260
// If we cannot get the type info in an expected way, describe the type.
258-
diagnose(BI->getModule().getASTContext(),
259-
Loc.getSourceLoc(),
260-
diag::arithmetic_operation_overflow_generic_type,
261-
LHSInt.toString(/*Radix*/ 10, Signed),
262-
Operator,
263-
RHSInt.toString(/*Radix*/ 10, Signed),
264-
Signed,
265-
LHSInt.getBitWidth()).highlight(LHSRange).highlight(RHSRange);
261+
diagnose(BI->getModule().getASTContext(), Loc.getSourceLoc(),
262+
diag::arithmetic_operation_overflow_generic_type, LhsStr,
263+
Operator, RhsStr, Signed, LHSInt.getBitWidth())
264+
.highlight(LHSRange)
265+
.highlight(RHSRange);
266266
}
267267
ResultsInError = Optional<bool>(true);
268268
}
@@ -570,12 +570,11 @@ constantFoldAndCheckDivision(BuiltinInst *BI, BuiltinValueKind ID,
570570

571571
// Otherwise emit the diagnostic, set ResultsInError to be true, and return
572572
// nullptr.
573-
diagnose(M.getASTContext(),
574-
BI->getLoc().getSourceLoc(),
573+
diagnose(M.getASTContext(), BI->getLoc().getSourceLoc(),
575574
diag::division_overflow,
576-
NumVal.toString(/*Radix*/ 10, /*Signed*/true),
575+
llvm::toString(NumVal, /*Radix*/ 10, /*Signed*/ true),
577576
IsRem ? "%" : "/",
578-
DenomVal.toString(/*Radix*/ 10, /*Signed*/true));
577+
llvm::toString(DenomVal, /*Radix*/ 10, /*Signed*/ true));
579578
ResultsInError = Optional<bool>(true);
580579
return nullptr;
581580
}
@@ -1366,7 +1365,7 @@ case BuiltinValueKind::id:
13661365
if (VInt.isNegative() && ResultsInError.hasValue()) {
13671366
diagnose(M.getASTContext(), BI->getLoc().getSourceLoc(),
13681367
diag::wrong_non_negative_assumption,
1369-
VInt.toString(/*Radix*/ 10, /*Signed*/ true));
1368+
llvm::toString(VInt, /*Radix*/ 10, /*Signed*/ true));
13701369
ResultsInError = Optional<bool>(true);
13711370
}
13721371
return V;

lib/Serialization/SerializeSIL.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,26 +1555,26 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
15551555
case SILInstructionKind::FloatLiteralInst:
15561556
case SILInstructionKind::IntegerLiteralInst: {
15571557
// Use SILOneOperandLayout to specify the type and the literal.
1558-
std::string Str;
1558+
llvm::SmallString<10> Str;
15591559
SILType Ty;
15601560
switch (SI.getKind()) {
15611561
default: llvm_unreachable("Out of sync with parent switch");
15621562
case SILInstructionKind::IntegerLiteralInst:
1563-
Str = cast<IntegerLiteralInst>(&SI)->getValue().toString(10, true);
1563+
cast<IntegerLiteralInst>(&SI)->getValue().toString(Str, 10,
1564+
/*signed*/ true);
15641565
Ty = cast<IntegerLiteralInst>(&SI)->getType();
15651566
break;
15661567
case SILInstructionKind::FloatLiteralInst:
1567-
Str = cast<FloatLiteralInst>(&SI)->getBits().toString(16,
1568-
/*Signed*/false);
1568+
cast<IntegerLiteralInst>(&SI)->getValue().toString(Str, 16,
1569+
/*signed*/ true);
15691570
Ty = cast<FloatLiteralInst>(&SI)->getType();
15701571
break;
15711572
}
15721573
unsigned abbrCode = SILAbbrCodes[SILOneOperandLayout::Code];
1573-
SILOneOperandLayout::emitRecord(Out, ScratchRecord, abbrCode,
1574-
(unsigned)SI.getKind(), 0,
1575-
S.addTypeRef(Ty.getASTType()),
1576-
(unsigned)Ty.getCategory(),
1577-
S.addUniquedStringRef(Str));
1574+
SILOneOperandLayout::emitRecord(
1575+
Out, ScratchRecord, abbrCode, (unsigned)SI.getKind(), 0,
1576+
S.addTypeRef(Ty.getASTType()), (unsigned)Ty.getCategory(),
1577+
S.addUniquedStringRef(Str.str()));
15781578
break;
15791579
}
15801580
case SILInstructionKind::MarkFunctionEscapeInst: {

0 commit comments

Comments
 (0)