Skip to content
Merged
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
19 changes: 6 additions & 13 deletions clang/lib/Lex/PPMacroExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,8 +1464,7 @@ static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,

/// Implements the __is_target_arch builtin macro.
static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
std::string ArchName = II->getName().lower() + "--";
llvm::Triple Arch(ArchName);
llvm::Triple Arch(II->getName().lower() + "--");
const llvm::Triple &TT = TI.getTriple();
if (TT.isThumb()) {
// arm matches thumb or thumbv7. armv7 matches thumbv7.
Expand Down Expand Up @@ -1494,9 +1493,7 @@ static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {

/// Implements the __is_target_os builtin macro.
static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
std::string OSName =
(llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
llvm::Triple OS(OSName);
llvm::Triple OS(llvm::Twine("unknown-unknown-") + II->getName().lower());
if (OS.getOS() == llvm::Triple::Darwin) {
// Darwin matches macos, ios, etc.
return TI.getTriple().isOSDarwin();
Expand All @@ -1507,12 +1504,11 @@ static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
/// Implements the __is_target_environment builtin macro.
static bool isTargetEnvironment(const TargetInfo &TI,
const IdentifierInfo *II) {
std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
llvm::Triple Env(EnvName);
llvm::Triple Env(llvm::Twine("---") + II->getName().lower());
// The unknown environment is matched only if
// '__is_target_environment(unknown)' is used.
if (Env.getEnvironment() == llvm::Triple::UnknownEnvironment &&
EnvName != "---unknown")
Env.getEnvironmentName() != "unknown")
return false;
return TI.getTriple().getEnvironment() == Env.getEnvironment();
}
Expand All @@ -1524,9 +1520,7 @@ static bool isTargetVariantOS(const TargetInfo &TI, const IdentifierInfo *II) {
if (!VariantTriple)
return false;

std::string OSName =
(llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
llvm::Triple OS(OSName);
llvm::Triple OS(llvm::Twine("unknown-unknown-") + II->getName().lower());
if (OS.getOS() == llvm::Triple::Darwin) {
// Darwin matches macos, ios, etc.
return VariantTriple->isOSDarwin();
Expand All @@ -1543,8 +1537,7 @@ static bool isTargetVariantEnvironment(const TargetInfo &TI,
const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
if (!VariantTriple)
return false;
std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
llvm::Triple Env(EnvName);
llvm::Triple Env(llvm::Twine("---") + II->getName().lower());
return VariantTriple->getEnvironment() == Env.getEnvironment();
}
return false;
Expand Down
8 changes: 7 additions & 1 deletion llvm/include/llvm/TargetParser/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_TARGETPARSER_TRIPLE_H
#define LLVM_TARGETPARSER_TRIPLE_H

#include "llvm/ADT/Twine.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/VersionTuple.h"

Expand All @@ -20,6 +20,7 @@
#undef sparc

namespace llvm {
class Twine;

/// Triple - Helper class for working with autoconf configuration names. For
/// historical reasons, we also call these 'triples' (they used to contain
Expand Down Expand Up @@ -349,7 +350,12 @@ class Triple {
/// triple fields unknown.
Triple() = default;

LLVM_ABI explicit Triple(std::string &&Str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why std::string && and const std::string & rather than one std::string ctor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding string copies, most of the uses (particularly the local uses) are just producing temporary strings that can be claimed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably my C++-foo is not strong enough, but I thought that doing a by-value pass + std::move gives you the same behavior (i.e. temporary or std::move argument doesn't get copied, everything else does).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is indeed the case. Technically there is an additional move, OTOH that pretty much never matters, since the optimizer is almost always perfectly capable of removing that.

explicit Triple(StringRef Str) : Triple(Str.str()) {}
explicit Triple(const char *Str) : Triple(std::string(Str)) {}
explicit Triple(const std::string &Str) : Triple(std::string(Str)) {}
Copy link
Member

@MaskRay MaskRay Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the StringRef overload (consider dropping explicit?), is Triple(const std::string &Str) still useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids ambiguous conversion to Twine or StringRef

LLVM_ABI explicit Triple(const Twine &Str);

LLVM_ABI Triple(const Twine &ArchStr, const Twine &VendorStr,
const Twine &OSStr);
LLVM_ABI Triple(const Twine &ArchStr, const Twine &VendorStr,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
if (parseToken(lltok::equal, "expected '=' after target triple") ||
parseStringConstant(Str))
return true;
M->setTargetTriple(Triple(Str));
M->setTargetTriple(Triple(std::move(Str)));
return false;
case lltok::kw_datalayout:
Lex.Lex();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4695,7 +4695,7 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
std::string S;
if (convertToString(Record, 0, S))
return error("Invalid record");
TheModule->setTargetTriple(Triple(S));
TheModule->setTargetTriple(Triple(std::move(S)));
break;
}
case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
return make_error<StringError>("createMCAsmBackend failed",
inconvertibleErrorCode());

Triple T(getTargetTriple().str());
Triple T(getTargetTriple());
AsmStreamer.reset(getTarget().createMCObjectStreamer(
T, Context, std::unique_ptr<MCAsmBackend>(MAB),
DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut)
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/MC/MCDisassembler/Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class LLVMDisasmContext {
MAI(std::move(MAI)), MRI(std::move(MRI)), MSI(std::move(MSI)),
MII(std::move(MII)), Ctx(std::move(Ctx)), DisAsm(std::move(DisAsm)),
IP(std::move(IP)), Options(0), CommentStream(CommentsToEmit) {}
const std::string &getTripleName() const { return TripleName; }
StringRef getTripleName() const { return TripleName; }
void *getDisInfo() const { return DisInfo; }
int getTagType() const { return TagType; }
LLVMOpInfoCallback getGetOpInfo() const { return GetOpInfo; }
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/MC/MCSectionELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/MC/MCSectionELF.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCExpr.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/MC/MCSubtargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCInstrItineraries.h"
#include "llvm/MC/MCSchedule.h"
#include "llvm/Support/Format.h"
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Object/ArchiveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ static bool isECObject(object::SymbolicFile &Obj) {
getBitcodeTargetTriple(Obj.getMemoryBufferRef());
if (!TripleStr)
return false;
Triple T(*TripleStr);
Triple T(std::move(*TripleStr));
return T.isWindowsArm64EC() || T.getArch() == Triple::x86_64;
}

Expand All @@ -719,7 +719,7 @@ static bool isAnyArm64COFF(object::SymbolicFile &Obj) {
getBitcodeTargetTriple(Obj.getMemoryBufferRef());
if (!TripleStr)
return false;
Triple T(*TripleStr);
Triple T(std::move(*TripleStr));
return T.isOSWindows() && T.getArch() == Triple::aarch64;
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
#define LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H

#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/Support/Alignment.h"
#include "llvm/TargetParser/Triple.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/TargetParser/CSKYTargetParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/TargetParser/CSKYTargetParser.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h"

using namespace llvm;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/TargetParser/LoongArchTargetParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/TargetParser/LoongArchTargetParser.h"
#include "llvm/ADT/SmallVector.h"

using namespace llvm;
using namespace llvm::LoongArch;
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/TargetParser/Triple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
///
/// This stores the string representation and parses the various pieces into
/// enum members.
Triple::Triple(const Twine &Str) : Data(Str.str()) {
Triple::Triple(std::string &&Str) : Data(std::move(Str)) {
// Do minimal parsing by hand here.
SmallVector<StringRef, 4> Components;
StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
Expand Down Expand Up @@ -1049,6 +1049,8 @@ Triple::Triple(const Twine &Str) : Data(Str.str()) {
ObjectFormat = getDefaultFormat(*this);
}

Triple::Triple(const Twine &Str) : Triple(Str.str()) {}

/// Construct a triple from string representations of the architecture,
/// vendor, and OS.
///
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/TargetParser/Unix/Host.inc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static std::string updateTripleOSVersion(std::string TargetTripleString) {
// On AIX, the AIX version and release should be that of the current host
// unless if the version has already been specified.
if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) {
Triple TT(TargetTripleString);
Triple TT(std::move(TargetTripleString));
if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) {
struct utsname name;
if (uname(&name) != -1) {
Expand Down