Skip to content

Commit 947b062

Browse files
committed
Reland "[Modules] No transitive source location change (#86912)"
This relands 6c31104. The patch was reverted due to incorrectly introduced alignment. And the patch was re-commited after fixing the alignment issue. Following off are the original message: This is part of "no transitive change" patch series, "no transitive source location change". I talked this with @Bigcheese in the tokyo's WG21 meeting. The idea comes from @jyknight posted on LLVM discourse. That for: ``` // A.cppm export module A; ... // B.cppm export module B; import A; ... //--- C.cppm export module C; import C; ``` Almost every time A.cppm changes, we need to recompile `B`. Due to we think the source location is significant to the semantics. But it may be good if we can avoid recompiling `C` if the change from `A` wouldn't change the BMI of B. This patch only cares source locations. So let's focus on source location's example. We can see the full example from the attached test. ``` //--- A.cppm export module A; export template <class T> struct C { T func() { return T(43); } }; export int funcA() { return 43; } //--- A.v1.cppm export module A; export template <class T> struct C { T func() { return T(43); } }; export int funcA() { return 43; } //--- B.cppm export module B; import A; export int funcB() { return funcA(); } //--- C.cppm export module C; import A; export void testD() { C<int> c; c.func(); } ``` Here the only difference between `A.cppm` and `A.v1.cppm` is that `A.v1.cppm` has an additional blank line. Then the test shows that two BMI of `B.cppm`, one specified `-fmodule-file=A=A.pcm` and the other specified `-fmodule-file=A=A.v1.pcm`, should have the bit-wise same contents. However, it is a different story for C, since C instantiates templates from A, and the instantiation records the source information from module A, which is different from `A` and `A.v1`, so it is expected that the BMI `C.pcm` and `C.v1.pcm` can and should differ. To fully understand the patch, we need to understand how we encodes source locations and how we serialize and deserialize them. For source locations, we encoded them as: ``` | | | _____ base offset of an imported module | | | |_____ base offset of another imported module | | | | | ___ 0 ``` As the diagram shows, we encode the local (unloaded) source location from 0 to higher bits. And we allocate the space for source locations from the loaded modules from high bits to 0. Then the source locations from the loaded modules will be mapped to our source location space according to the allocated offset. For example, for, ``` // a.cppm export module a; ... // b.cppm export module b; import a; ... ``` Assuming the offset of a source location (let's name the location as `S`) in a.cppm is 45 and we will record the value `45` into the BMI `a.pcm`. Then in b.cppm, when we import a, the source manager will allocate a space for module 'a' (according to the recorded number of source locations) as the base offset of module 'a' in the current source location spaces. Let's assume the allocated base offset as 90 in this example. Then when we want to get the location in the current source location space for `S`, we can get it simply by adding `45` to `90` to `135`. Finally we can get the source location for `S` in module B as `135`. And when we want to write module `b`, we would also write the source location of `S` as `135` directly in the BMI. And to clarify the location `S` comes from module `a`, we also need to record the base offset of module `a`, 90 in the BMI of `b`. Then the problem comes. Since the base offset of module 'a' is computed by the number source locations in module 'a'. In module 'b', the recorded base offset of module 'a' will change every time the number of source locations in module 'a' increase or decrease. In other words, the contents of BMI of B will change every time the number of locations in module 'a' changes. This is pretty sensitive. Almost every change will change the number of locations. So this is the problem this patch want to solve. Let's continue with the existing design to understand what's going on. Another interesting case is: ``` // c.cppm export module c; import whatever; import a; import b; ... ``` In `c.cppm`, when we import `a`, we still need to allocate a base location offset for it, let's say the value becomes to `200` somehow. Then when we reach the location `S` recorded in module `b`, we need to translate it into the current source location space. The solution is quite simple, we can get it by `135 + (200 - 90) = 245`. In another word, the offset of a source location in current module can be computed as `Recorded Offset + Base Offset of the its module file - Recorded Base Offset`. Then we're almost done about how we handle the offset of source locations in serializers. From the abstract level, what we want to do is to remove the hardcoded base offset of imported modules and remain the ability to calculate the source location in a new module unit. To achieve this, we need to be able to find the module file owning a source location from the encoding of the source location. So in this patch, for each source location, we will store the local offset of the location and the module file index. For the above example, in `b.pcm`, the source location of `S` will be recorded as `135` directly. And in the new design, the source location of `S` will be recorded as `<1, 45>`. Here `1` stands for the module file index of `a` in module `b`. And `45` means the offset of `S` to the base offset of module `a`. So the trade-off here is that, to make the BMI more independent, we need to record more abstract information. And I feel it is worthy. The recompilation problem of modules is really annoying and there are still people complaining this. But if we can make this (including stopping other changes transitively), I think this may be a killer feature for modules. And from @Bigcheese , this should be helpful for clang explicit modules too. And the benchmarking side, I tested this patch against https://github.com/alibaba/async_simple/tree/CXX20Modules. No significant change on compilation time. The size of .pcm files becomes to 204M from 200M. I think the trade-off is pretty fair. I didn't use another slot to record the module file index. I tried to use the higher 32 bits of the existing source location encodings to store that information. This design may be safe. Since we use `unsigned` to store source locations but we use uint64_t in serialization. And generally `unsigned` is 32 bit width in most platforms. So it might not be a safe problem. Since all the bits we used to store the module file index is not used before. So the new encodings may be: ``` |-----------------------|-----------------------| | A | B | C | * A: 32 bit. The index of the module file in the module manager + 1. * The +1 here is necessary since we wish 0 stands for the current module file. * B: 31 bit. The offset of the source location to the module file * containing it. * C: The macro bit. We rotate it to the lowest bit so that we can save * some space in case the index of the module file is 0. ``` (The B and C is the existing raw encoding for source locations) Another reason to reuse the same slot of the source location is to reduce the impact of the patch. Since there are a lot of places assuming we can store and get a source location from a slot. And if I tried to add another slot, a lot of codes breaks. I don't feel it is worhty. Another impact of this decision is that, the existing small optimizations for encoding source location may be invalided. The key of the optimization is that we can turn large values into small values then we can use VBR6 format to reduce the size. But if we decided to put the module file index into the higher bits, then maybe it simply doesn't work. An example may be the `SourceLocationSequence` optimization. This will only affect the size of on-disk .pcm files. I don't expect this impact the speed and memory use of compilations. And seeing my small experiments above, I feel this trade off is worthy. The mental model for handling source location offsets is not so complex and I believe we can solve it by adding module file index to each stored source location. For the practical side, since the source location is pretty sensitive, and the patch can pass all the in-tree tests and a small scale projects, I feel it should be correct. I'll continue to work on no transitive decl change and no transitive identifier change (if matters) to achieve the goal to stop the propagation of unnecessary changes. But all of this depends on this patch. Since, clearly, the source locations are the most sensitive thing. --- The release nots and documentation will be added seperately.
1 parent b944b54 commit 947b062

14 files changed

+326
-174
lines changed

clang/include/clang/Basic/SourceLocation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class SourceLocation {
9090
friend class ASTWriter;
9191
friend class SourceManager;
9292
friend struct llvm::FoldingSetTrait<SourceLocation, void>;
93+
friend class SourceLocationEncoding;
9394

9495
public:
9596
using UIntTy = uint32_t;

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/Basic/IdentifierTable.h"
2424
#include "clang/Basic/OperatorKinds.h"
2525
#include "clang/Basic/SourceLocation.h"
26+
#include "clang/Serialization/SourceLocationEncoding.h"
2627
#include "llvm/ADT/DenseMapInfo.h"
2728
#include "llvm/Bitstream/BitCodes.h"
2829
#include <cassert>
@@ -165,99 +166,95 @@ using SubmoduleID = uint32_t;
165166
/// The number of predefined submodule IDs.
166167
const unsigned int NUM_PREDEF_SUBMODULE_IDS = 1;
167168

169+
/// 32 aligned uint64_t in the AST file. Use splitted 64-bit integer into
170+
/// low/high parts to keep structure alignment 32-bit (it is important
171+
/// because blobs in bitstream are 32-bit aligned). This structure is
172+
/// serialized "as is" to the AST file.
173+
class UnalignedUInt64 {
174+
uint32_t BitLow = 0;
175+
uint32_t BitHigh = 0;
176+
177+
public:
178+
UnalignedUInt64() = default;
179+
UnalignedUInt64(uint64_t BitOffset) { set(BitOffset); }
180+
181+
void set(uint64_t Offset) {
182+
BitLow = Offset;
183+
BitHigh = Offset >> 32;
184+
}
185+
186+
uint64_t get() const { return BitLow | (uint64_t(BitHigh) << 32); }
187+
};
188+
168189
/// Source range/offset of a preprocessed entity.
169-
struct PPEntityOffset {
190+
class PPEntityOffset {
191+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
192+
170193
/// Raw source location of beginning of range.
171-
SourceLocation::UIntTy Begin;
194+
UnalignedUInt64 Begin;
172195

173196
/// Raw source location of end of range.
174-
SourceLocation::UIntTy End;
197+
UnalignedUInt64 End;
175198

176199
/// Offset in the AST file relative to ModuleFile::MacroOffsetsBase.
177200
uint32_t BitOffset;
178201

179-
PPEntityOffset(SourceRange R, uint32_t BitOffset)
180-
: Begin(R.getBegin().getRawEncoding()), End(R.getEnd().getRawEncoding()),
181-
BitOffset(BitOffset) {}
202+
public:
203+
PPEntityOffset(RawLocEncoding Begin, RawLocEncoding End, uint32_t BitOffset)
204+
: Begin(Begin), End(End), BitOffset(BitOffset) {}
182205

183-
SourceLocation getBegin() const {
184-
return SourceLocation::getFromRawEncoding(Begin);
185-
}
206+
RawLocEncoding getBegin() const { return Begin.get(); }
207+
RawLocEncoding getEnd() const { return End.get(); }
186208

187-
SourceLocation getEnd() const {
188-
return SourceLocation::getFromRawEncoding(End);
189-
}
209+
uint32_t getOffset() const { return BitOffset; }
190210
};
191211

192212
/// Source range of a skipped preprocessor region
193-
struct PPSkippedRange {
213+
class PPSkippedRange {
214+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
215+
194216
/// Raw source location of beginning of range.
195-
SourceLocation::UIntTy Begin;
217+
UnalignedUInt64 Begin;
196218
/// Raw source location of end of range.
197-
SourceLocation::UIntTy End;
219+
UnalignedUInt64 End;
198220

199-
PPSkippedRange(SourceRange R)
200-
: Begin(R.getBegin().getRawEncoding()), End(R.getEnd().getRawEncoding()) {
201-
}
221+
public:
222+
PPSkippedRange(RawLocEncoding Begin, RawLocEncoding End)
223+
: Begin(Begin), End(End) {}
202224

203-
SourceLocation getBegin() const {
204-
return SourceLocation::getFromRawEncoding(Begin);
205-
}
206-
SourceLocation getEnd() const {
207-
return SourceLocation::getFromRawEncoding(End);
208-
}
225+
RawLocEncoding getBegin() const { return Begin.get(); }
226+
RawLocEncoding getEnd() const { return End.get(); }
209227
};
210228

211-
/// Offset in the AST file. Use splitted 64-bit integer into low/high
212-
/// parts to keep structure alignment 32-bit (it is important because
213-
/// blobs in bitstream are 32-bit aligned). This structure is serialized
214-
/// "as is" to the AST file.
215-
struct UnderalignedInt64 {
216-
uint32_t BitOffsetLow = 0;
217-
uint32_t BitOffsetHigh = 0;
218-
219-
UnderalignedInt64() = default;
220-
UnderalignedInt64(uint64_t BitOffset) { setBitOffset(BitOffset); }
221-
222-
void setBitOffset(uint64_t Offset) {
223-
BitOffsetLow = Offset;
224-
BitOffsetHigh = Offset >> 32;
225-
}
226-
227-
uint64_t getBitOffset() const {
228-
return BitOffsetLow | (uint64_t(BitOffsetHigh) << 32);
229-
}
230-
};
229+
/// Source location and bit offset of a declaration. Keep
230+
/// structure alignment 32-bit since the blob is assumed as 32-bit aligned.
231+
class DeclOffset {
232+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
231233

232-
/// Source location and bit offset of a declaration.
233-
struct DeclOffset {
234234
/// Raw source location.
235-
SourceLocation::UIntTy Loc = 0;
235+
UnalignedUInt64 RawLoc;
236236

237-
/// Offset relative to the start of the DECLTYPES_BLOCK block. Keep
238-
/// structure alignment 32-bit and avoid padding gap because undefined
239-
/// value in the padding affects AST hash.
240-
UnderalignedInt64 BitOffset;
237+
/// Offset relative to the start of the DECLTYPES_BLOCK block.
238+
UnalignedUInt64 BitOffset;
241239

240+
public:
242241
DeclOffset() = default;
243-
DeclOffset(SourceLocation Loc, uint64_t BitOffset,
244-
uint64_t DeclTypesBlockStartOffset) {
245-
setLocation(Loc);
242+
DeclOffset(RawLocEncoding RawLoc, uint64_t BitOffset,
243+
uint64_t DeclTypesBlockStartOffset)
244+
: RawLoc(RawLoc) {
246245
setBitOffset(BitOffset, DeclTypesBlockStartOffset);
247246
}
248247

249-
void setLocation(SourceLocation L) { Loc = L.getRawEncoding(); }
248+
void setRawLoc(RawLocEncoding Loc) { RawLoc = Loc; }
250249

251-
SourceLocation getLocation() const {
252-
return SourceLocation::getFromRawEncoding(Loc);
253-
}
250+
RawLocEncoding getRawLoc() const { return RawLoc.get(); }
254251

255252
void setBitOffset(uint64_t Offset, const uint64_t DeclTypesBlockStartOffset) {
256-
BitOffset.setBitOffset(Offset - DeclTypesBlockStartOffset);
253+
BitOffset.set(Offset - DeclTypesBlockStartOffset);
257254
}
258255

259256
uint64_t getBitOffset(const uint64_t DeclTypesBlockStartOffset) const {
260-
return BitOffset.getBitOffset() + DeclTypesBlockStartOffset;
257+
return BitOffset.get() + DeclTypesBlockStartOffset;
261258
}
262259
};
263260

clang/include/clang/Serialization/ASTReader.h

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,7 @@ class ASTReader
17711771

17721772
/// Retrieve the module manager.
17731773
ModuleManager &getModuleManager() { return ModuleMgr; }
1774+
const ModuleManager &getModuleManager() const { return ModuleMgr; }
17741775

17751776
/// Retrieve the preprocessor.
17761777
Preprocessor &getPreprocessor() const { return PP; }
@@ -2177,8 +2178,8 @@ class ASTReader
21772178

21782179
/// Retrieve the global submodule ID given a module and its local ID
21792180
/// number.
2180-
serialization::SubmoduleID
2181-
getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
2181+
serialization::SubmoduleID getGlobalSubmoduleID(ModuleFile &M,
2182+
unsigned LocalID) const;
21822183

21832184
/// Retrieve the submodule that corresponds to a global submodule ID.
21842185
///
@@ -2191,7 +2192,7 @@ class ASTReader
21912192

21922193
/// Retrieve the module file with a given local ID within the specified
21932194
/// ModuleFile.
2194-
ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID);
2195+
ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID) const;
21952196

21962197
/// Get an ID for the given module file.
21972198
unsigned getModuleFileID(ModuleFile *M);
@@ -2227,33 +2228,46 @@ class ASTReader
22272228
return Sema::AlignPackInfo::getFromRawEncoding(Raw);
22282229
}
22292230

2231+
using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
2232+
22302233
/// Read a source location from raw form and return it in its
22312234
/// originating module file's source location space.
2232-
SourceLocation ReadUntranslatedSourceLocation(SourceLocation::UIntTy Raw,
2233-
LocSeq *Seq = nullptr) const {
2235+
std::pair<SourceLocation, unsigned>
2236+
ReadUntranslatedSourceLocation(RawLocEncoding Raw,
2237+
LocSeq *Seq = nullptr) const {
22342238
return SourceLocationEncoding::decode(Raw, Seq);
22352239
}
22362240

22372241
/// Read a source location from raw form.
2238-
SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2239-
SourceLocation::UIntTy Raw,
2242+
SourceLocation ReadSourceLocation(ModuleFile &MF, RawLocEncoding Raw,
22402243
LocSeq *Seq = nullptr) const {
2241-
SourceLocation Loc = ReadUntranslatedSourceLocation(Raw, Seq);
2242-
return TranslateSourceLocation(ModuleFile, Loc);
2244+
if (!MF.ModuleOffsetMap.empty())
2245+
ReadModuleOffsetMap(MF);
2246+
2247+
auto [Loc, ModuleFileIndex] = ReadUntranslatedSourceLocation(Raw, Seq);
2248+
ModuleFile *OwningModuleFile =
2249+
ModuleFileIndex == 0 ? &MF : MF.DependentModules[ModuleFileIndex - 1];
2250+
2251+
assert(!SourceMgr.isLoadedSourceLocation(Loc) &&
2252+
"Run out source location space");
2253+
2254+
return TranslateSourceLocation(*OwningModuleFile, Loc);
22432255
}
22442256

22452257
/// Translate a source location from another module file's source
22462258
/// location space into ours.
22472259
SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile,
22482260
SourceLocation Loc) const {
2249-
if (!ModuleFile.ModuleOffsetMap.empty())
2250-
ReadModuleOffsetMap(ModuleFile);
2251-
assert(ModuleFile.SLocRemap.find(Loc.getOffset()) !=
2252-
ModuleFile.SLocRemap.end() &&
2253-
"Cannot find offset to remap.");
2254-
SourceLocation::IntTy Remap =
2255-
ModuleFile.SLocRemap.find(Loc.getOffset())->second;
2256-
return Loc.getLocWithOffset(Remap);
2261+
if (Loc.isInvalid())
2262+
return Loc;
2263+
2264+
// FIXME: TranslateSourceLocation is not re-enterable. It is problematic
2265+
// to call TranslateSourceLocation on a translated source location.
2266+
// We either need a method to know whether or not a source location is
2267+
// translated or refactor the code to make it clear that
2268+
// TranslateSourceLocation won't be called with translated source location.
2269+
2270+
return Loc.getLocWithOffset(ModuleFile.SLocEntryBaseOffset - 2);
22572271
}
22582272

22592273
/// Read a source location.

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class ASTWriter : public ASTDeserializationListener,
274274

275275
/// Offset of each type in the bitstream, indexed by
276276
/// the type's ID.
277-
std::vector<serialization::UnderalignedInt64> TypeOffsets;
277+
std::vector<serialization::UnalignedUInt64> TypeOffsets;
278278

279279
/// The first ID number we can use for our own identifiers.
280280
serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
@@ -676,6 +676,10 @@ class ASTWriter : public ASTDeserializationListener,
676676
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
677677
LocSeq *Seq = nullptr);
678678

679+
/// Return the raw encodings for source locations.
680+
SourceLocationEncoding::RawLocEncoding
681+
getRawSourceLocationEncoding(SourceLocation Loc, LocSeq *Seq = nullptr);
682+
679683
/// Emit a source range.
680684
void AddSourceRange(SourceRange Range, RecordDataImpl &Record,
681685
LocSeq *Seq = nullptr);

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,6 @@ class ModuleFile {
295295
/// AST file.
296296
const uint32_t *SLocEntryOffsets = nullptr;
297297

298-
/// Remapping table for source locations in this module.
299-
ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 2>
300-
SLocRemap;
301-
302298
// === Identifiers ===
303299

304300
/// The number of identifiers in this AST file.
@@ -495,7 +491,7 @@ class ModuleFile {
495491

496492
/// Offset of each type within the bitstream, indexed by the
497493
/// type ID, or the representation of a Type*.
498-
const UnderalignedInt64 *TypeOffsets = nullptr;
494+
const UnalignedUInt64 *TypeOffsets = nullptr;
499495

500496
/// Base type ID for types local to this module as represented in
501497
/// the global type ID space.
@@ -512,9 +508,17 @@ class ModuleFile {
512508
/// List of modules which depend on this module
513509
llvm::SetVector<ModuleFile *> ImportedBy;
514510

515-
/// List of modules which this module depends on
511+
/// List of modules which this module directly imported
516512
llvm::SetVector<ModuleFile *> Imports;
517513

514+
/// List of modules which this modules dependent on. Different
515+
/// from `Imports`, this includes indirectly imported modules too.
516+
/// The order of DependentModules is significant. It should keep
517+
/// the same order with that module file manager when we write
518+
/// the current module file. The value of the member will be initialized
519+
/// in `ASTReader::ReadModuleOffsetMap`.
520+
llvm::SmallVector<ModuleFile *, 16> DependentModules;
521+
518522
/// Determine whether this module was directly imported at
519523
/// any point during translation.
520524
bool isDirectlyImported() const { return DirectlyImported; }

0 commit comments

Comments
 (0)