Author: Timm Baeder Date: 2026-08-12T13:14:28+02:00 New Revision: 5bf59e2c4b54a85e4e7f0e188b99061beb2708f6
URL: https://github.com/llvm/llvm-project/commit/5bf59e2c4b54a85e4e7f0e188b99061beb2708f6 DIFF: https://github.com/llvm/llvm-project/commit/5bf59e2c4b54a85e4e7f0e188b99061beb2708f6.diff LOG: [clang][bytecode] Make `SourceMap` a proper class (#215760) Instead of a typedef. Save offsets and infos separately to speed up the binary search a bit. Added: Modified: clang/lib/AST/ByteCode/ByteCodeEmitter.cpp clang/lib/AST/ByteCode/Function.cpp clang/lib/AST/ByteCode/Source.h Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp index 0a8a766ed8a4e..9a2c1c2b496b0 100644 --- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp +++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp @@ -213,9 +213,9 @@ bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) { // attached to the address after the opcode. emit(P, Code, Op, Success); if (LocOverride) - SrcMap.emplace_back(Code.size(), *LocOverride); + SrcMap.push(Code.size(), *LocOverride); else if (SI) - SrcMap.emplace_back(Code.size(), SI); + SrcMap.push(Code.size(), SI); (..., emit(P, Code, Args, Success)); return Success; diff --git a/clang/lib/AST/ByteCode/Function.cpp b/clang/lib/AST/ByteCode/Function.cpp index 22d26e498c5b6..49282c9dc7a33 100644 --- a/clang/lib/AST/ByteCode/Function.cpp +++ b/clang/lib/AST/ByteCode/Function.cpp @@ -64,9 +64,5 @@ SourceInfo Function::getSource(CodePtr PC) const { assert(PC <= getCodeEnd() && "PC Does not belong to this function"); assert(hasBody() && "Function has no body"); unsigned Offset = PC - getCodeBegin(); - using Elem = std::pair<unsigned, SourceInfo>; - auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first()); - if (It == SrcMap.end()) - return SrcMap.back().second; - return It->second; + return SrcMap.findSourceForOffset(Offset); } diff --git a/clang/lib/AST/ByteCode/Source.h b/clang/lib/AST/ByteCode/Source.h index 32bc7856068fe..db935da7c7c49 100644 --- a/clang/lib/AST/ByteCode/Source.h +++ b/clang/lib/AST/ByteCode/Source.h @@ -17,6 +17,7 @@ #include "clang/AST/DeclBase.h" #include "clang/AST/Stmt.h" #include "llvm/ADT/PointerUnion.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Support/Endian.h" namespace clang { @@ -97,7 +98,30 @@ class SourceInfo final { }; static_assert(sizeof(SourceInfo) == sizeof(void *)); -using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>; +// A map from byte code offset to source information. +// This is used to get the location in the input source file for diagnostics. +class SourceMap final { +private: + llvm::SmallVector<uint32_t> Offsets; + llvm::SmallVector<SourceInfo> Infos; + +public: + SourceMap() = default; + void push(uint32_t Offset, SourceInfo Info) { + Offsets.push_back(Offset); + Infos.push_back(Info); + } + + SourceInfo findSourceForOffset(uint32_t Offset) const { + assert(!Offsets.empty()); + assert(Offsets.size() == Infos.size()); +#ifndef NDEBUG + assert(llvm::is_sorted(Offsets)); +#endif + const auto *It = llvm::lower_bound(Offsets, Offset); + return Infos[It - Offsets.begin()]; + } +}; /// Interface for classes which map locations to sources. class SourceMapper { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
