Skip to content

Commit 1418127

Browse files
nikicnavaneethshan
authored andcommitted
Reapply [ConstantInt] Disable implicit truncation in ConstantInt::get() (#171456)
Reapply after additional fixes in #174426 and #174431. ----- Disable implicit truncation in the ConstantInt constructor by default. This means that it needs to be passed a signed/unsigned (depending on the IsSigned flag) value matching the bit width. The intention is to prevent the recurring bug where people write something like `ConstantInt::get(Ty, -1)`, and this "works" until `Ty` is larger than 64-bit and then the value is incorrect due to missing type extension. This is the continuation of llvm/llvm-project#112670, which originally allowed implicit truncation in this constructor to reduce initial scope of the change.
1 parent 8859f76 commit 1418127

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

llvm/include/llvm/IR/Constants.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,30 @@ class ConstantInt final : public ConstantData {
113113
/// If Ty is a vector type, return a Constant with a splat of the given
114114
/// value. Otherwise return a ConstantInt for the given value.
115115
/// \param ImplicitTrunc Whether to allow implicit truncation of the value.
116-
// TODO: Make ImplicitTrunc default to false.
117116
LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false,
118-
bool ImplicitTrunc = true);
117+
bool ImplicitTrunc = false);
119118

120119
/// Return a ConstantInt with the specified integer value for the specified
121120
/// type. If the type is wider than 64 bits, the value will be zero-extended
122121
/// to fit the type, unless IsSigned is true, in which case the value will
123122
/// be interpreted as a 64-bit signed integer and sign-extended to fit
124123
/// the type.
125124
/// \param ImplicitTrunc Whether to allow implicit truncation of the value.
126-
// TODO: Make ImplicitTrunc default to false.
127125
LLVM_ABI static ConstantInt *get(IntegerType *Ty, uint64_t V,
128126
bool IsSigned = false,
129-
bool ImplicitTrunc = true);
127+
bool ImplicitTrunc = false);
130128

131129
/// Return a ConstantInt with the specified value for the specified type. The
132130
/// value V will be canonicalized to an unsigned APInt. Accessing it with
133131
/// either getSExtValue() or getZExtValue() will yield a correctly sized and
134132
/// signed value for the type Ty.
135133
/// Get a ConstantInt for a specific signed value.
136134
/// \param ImplicitTrunc Whether to allow implicit truncation of the value.
137-
// TODO: Make ImplicitTrunc default to false.
138135
static ConstantInt *getSigned(IntegerType *Ty, int64_t V,
139-
bool ImplicitTrunc = true) {
136+
bool ImplicitTrunc = false) {
140137
return get(Ty, V, /*IsSigned=*/true, ImplicitTrunc);
141138
}
142-
static Constant *getSigned(Type *Ty, int64_t V, bool ImplicitTrunc = true) {
139+
static Constant *getSigned(Type *Ty, int64_t V, bool ImplicitTrunc = false) {
143140
return get(Ty, V, /*IsSigned=*/true, ImplicitTrunc);
144141
}
145142

0 commit comments

Comments
 (0)