Skip to content

Commit 5226f3d

Browse files
authored
Factor out GetInstWithConstantValue and use it from another place that duplicates the same logic. (#5388)
1 parent 98ee8f3 commit 5226f3d

File tree

4 files changed

+48
-46
lines changed

4 files changed

+48
-46
lines changed

toolchain/check/import_ref.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -152,35 +152,6 @@ auto VerifySameCanonicalImportIRInst(Context& context, SemIR::NameId name_id,
152152
SemIR::LocId(prev_id));
153153
}
154154

155-
// Returns an instruction that has the specified constant value.
156-
static auto GetInstWithConstantValue(const SemIR::File& file,
157-
SemIR::ConstantId const_id)
158-
-> SemIR::InstId {
159-
if (!const_id.has_value()) {
160-
return SemIR::InstId::None;
161-
}
162-
163-
// For concrete constants, the corresponding instruction has the desired
164-
// constant value.
165-
if (!const_id.is_symbolic()) {
166-
return file.constant_values().GetInstId(const_id);
167-
}
168-
169-
// For abstract symbolic constants, the corresponding instruction has the
170-
// desired constant value.
171-
const auto& symbolic_const =
172-
file.constant_values().GetSymbolicConstant(const_id);
173-
if (!symbolic_const.generic_id.has_value()) {
174-
return file.constant_values().GetInstId(const_id);
175-
}
176-
177-
// For a symbolic constant in a generic, pick the corresponding instruction
178-
// out of the eval block for the generic.
179-
const auto& generic = file.generics().Get(symbolic_const.generic_id);
180-
auto block = generic.GetEvalBlock(symbolic_const.index.region());
181-
return file.inst_blocks().Get(block)[symbolic_const.index.index()];
182-
}
183-
184155
namespace {
185156
class ImportRefResolver;
186157

toolchain/sem_ir/constant.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,31 @@ auto ConstantStore::GetOrAdd(Inst inst, ConstantDependence dependence)
3636
return result.value();
3737
}
3838

39+
auto GetInstWithConstantValue(const SemIR::File& file,
40+
SemIR::ConstantId const_id) -> SemIR::InstId {
41+
if (!const_id.has_value() || !const_id.is_constant()) {
42+
return SemIR::InstId::None;
43+
}
44+
45+
// For concrete constants, the corresponding instruction has the desired
46+
// constant value.
47+
if (!const_id.is_symbolic()) {
48+
return file.constant_values().GetInstId(const_id);
49+
}
50+
51+
// For unattached symbolic constants, the corresponding instruction has the
52+
// desired constant value.
53+
const auto& symbolic_const =
54+
file.constant_values().GetSymbolicConstant(const_id);
55+
if (!symbolic_const.generic_id.has_value()) {
56+
return file.constant_values().GetInstId(const_id);
57+
}
58+
59+
// For attached symbolic constants, pick the corresponding instruction out of
60+
// the eval block for the generic.
61+
const auto& generic = file.generics().Get(symbolic_const.generic_id);
62+
auto block = generic.GetEvalBlock(symbolic_const.index.region());
63+
return file.inst_blocks().Get(block)[symbolic_const.index.index()];
64+
}
65+
3966
} // namespace Carbon::SemIR

toolchain/sem_ir/constant.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ class ConstantValueStore {
187187
llvm::SmallVector<SymbolicConstant, 0> symbolic_constants_;
188188
};
189189

190+
// Given a constant ID, returns an instruction that has that constant value.
191+
// For an unattached constant, the returned instruction is the instruction that
192+
// defines the constant; for an attached constant, this is the instruction in
193+
// the eval block that computes the constant value in each specific.
194+
//
195+
// Returns InstId::None if the ConstantId is None or NotConstant.
196+
auto GetInstWithConstantValue(const SemIR::File& file,
197+
SemIR::ConstantId const_id) -> SemIR::InstId;
198+
190199
// Provides storage for instructions representing deduplicated global constants.
191200
class ConstantStore {
192201
public:

toolchain/sem_ir/formatter.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,24 +1271,19 @@ auto Formatter::FormatConstant(ConstantId id) -> void {
12711271
return;
12721272
}
12731273

1274-
// For a symbolic constant in a generic, list the constant value in the
1275-
// generic first, and the canonical constant second.
1276-
if (id.is_symbolic()) {
1277-
const auto& symbolic_constant =
1278-
sem_ir_->constant_values().GetSymbolicConstant(id);
1279-
if (symbolic_constant.generic_id.has_value()) {
1280-
const auto& generic =
1281-
sem_ir_->generics().Get(symbolic_constant.generic_id);
1282-
FormatName(sem_ir_->inst_blocks().Get(generic.GetEvalBlock(
1283-
symbolic_constant.index.region()))[symbolic_constant.index.index()]);
1284-
out_ << " (";
1285-
FormatName(sem_ir_->constant_values().GetInstId(id));
1286-
out_ << ")";
1287-
return;
1288-
}
1289-
}
1274+
auto inst_id = GetInstWithConstantValue(*sem_ir_, id);
1275+
FormatName(inst_id);
12901276

1291-
FormatName(sem_ir_->constant_values().GetInstId(id));
1277+
// For an attached constant, also list the unattached constant.
1278+
if (id.is_symbolic() && sem_ir_->constant_values()
1279+
.GetSymbolicConstant(id)
1280+
.generic_id.has_value()) {
1281+
// TODO: Skip printing this if it's the same as `inst_id`.
1282+
auto unattached_inst_id = sem_ir_->constant_values().GetInstId(id);
1283+
out_ << " (";
1284+
FormatName(unattached_inst_id);
1285+
out_ << ")";
1286+
}
12921287
}
12931288

12941289
auto Formatter::FormatInstAsType(InstId id) -> void {

0 commit comments

Comments
 (0)