================ @@ -156,19 +157,51 @@ class OffloadBinary : public Binary { /// owns its memory. class OffloadFile : public OwningBinary<OffloadBinary> { public: + /// An ordered pair of the target triple and the architecture. using TargetID = std::pair<StringRef, StringRef>; OffloadFile(std::unique_ptr<OffloadBinary> Binary, std::unique_ptr<MemoryBuffer> Buffer) : OwningBinary<OffloadBinary>(std::move(Binary), std::move(Buffer)) {} + Expected<OffloadFile> copy() const { + std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBufferCopy( + getBinary()->getMemoryBufferRef().getBuffer()); + auto NewBinaryOrErr = OffloadBinary::create(*Buffer); + if (!NewBinaryOrErr) + return NewBinaryOrErr.takeError(); + return OffloadFile(std::move(*NewBinaryOrErr), std::move(Buffer)); + } + /// We use the Triple and Architecture pair to group linker inputs together. /// This conversion function lets us use these inputs in a hash-map. operator TargetID() const { return std::make_pair(getBinary()->getTriple(), getBinary()->getArch()); } }; +/// Queries if the target \p LHS is compatible with \p RHS for linking purposes. +inline bool areTargetsCompatible(const OffloadFile::TargetID LHS, + const OffloadFile::TargetID RHS) { + if (LHS == RHS) + return true; + + // If the target is AMD we check the target IDs for compatibility. A target id + // is a string conforming to the folowing BNF syntax: + // + // target-id ::= '<arch> ( : <feature> ( '+' | '-' ) )*' + // + // This is used to link mutually compatible architectures together. + llvm::Triple T(LHS.first); + if (!T.isAMDGPU()) + return false; + + // The targets are compatible if the architecture is a subset of the other. + if (RHS.second.contains(LHS.second)) + return true; + return false; ---------------- arsenm wrote:
return RHS.second.contains https://github.com/llvm/llvm-project/pull/72442 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits