Skip to content

Commit ca8e3d7

Browse files
committed
Reactor: Fix printing of small integer values.
The printf specifiers expect an int, passing a smaller width integer could lead to weirdness being printed. Change-Id: I1e75e3fdade270a4bfa55c48efe82ca9f331b2fe Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31272 Presubmit-Ready: Ben Clayton <bclayton@google.com> Tested-by: Ben Clayton <bclayton@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: Nicolas Capens <nicolascapens@google.com> Reviewed-by: Chris Forbes <chrisforbes@google.com>
1 parent e51f859 commit ca8e3d7

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/Reactor/LLVMReactor.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,6 +4103,27 @@ namespace rr
41034103
return elements;
41044104
}
41054105

4106+
// toInt returns all the integer values in vals extended to a native width
4107+
// integer.
4108+
static std::vector<Value*> toInt(const std::vector<Value*>& vals, bool isSigned)
4109+
{
4110+
auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width.
4111+
std::vector<Value*> elements;
4112+
elements.reserve(vals.size());
4113+
for (auto v : vals)
4114+
{
4115+
if (isSigned)
4116+
{
4117+
elements.push_back(V(::builder->CreateSExt(V(v), intTy)));
4118+
}
4119+
else
4120+
{
4121+
elements.push_back(V(::builder->CreateZExt(V(v), intTy)));
4122+
}
4123+
}
4124+
return elements;
4125+
}
4126+
41064127
// toDouble returns all the float values in vals extended to doubles.
41074128
static std::vector<Value*> toDouble(const std::vector<Value*>& vals)
41084129
{
@@ -4116,11 +4137,15 @@ namespace rr
41164137
return elements;
41174138
}
41184139

4119-
std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return extractAll(v.value, 4); }
4120-
std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return extractAll(v.value, 4); }
4121-
std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return extractAll(v.value, 4); }
4122-
std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return extractAll(v.value, 4); }
4123-
std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return extractAll(v.value, 4); }
4140+
std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); }
4141+
std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); }
4142+
std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); }
4143+
std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return toInt(extractAll(v.value, 4), true); }
4144+
std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); }
4145+
std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); }
4146+
std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); }
4147+
std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); }
4148+
std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return toInt(extractAll(v.value, 4), false); }
41244149
std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); }
41254150
std::vector<Value*> PrintValue::Ty<Float4>::val(const RValue<Float4>& v) { return toDouble(extractAll(v.value, 4)); }
41264151
std::vector<Value*> PrintValue::Ty<const char*>::val(const char* v) { return {V(::builder->CreateGlobalStringPtr(v))}; }
@@ -4129,7 +4154,7 @@ namespace rr
41294154
{
41304155
// LLVM types used below.
41314156
auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
4132-
auto intTy = ::llvm::Type::getInt64Ty(*::context); // TODO: Natural int width.
4157+
auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width.
41334158
auto i8PtrTy = ::llvm::Type::getInt8PtrTy(*::context);
41344159
auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true);
41354160

src/Reactor/Reactor.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,7 +3328,12 @@ namespace rr
33283328
template <> struct PrintValue::Ty<Int>
33293329
{
33303330
static constexpr const char* fmt = "%d";
3331-
static std::vector<Value*> val(const RValue<Int>& v) { return {v.value}; }
3331+
static std::vector<Value*> val(const RValue<Int>& v);
3332+
};
3333+
template <> struct PrintValue::Ty<Int2>
3334+
{
3335+
static constexpr const char* fmt = "[%d, %d]";
3336+
static std::vector<Value*> val(const RValue<Int2>& v);
33323337
};
33333338
template <> struct PrintValue::Ty<Int4>
33343339
{
@@ -3338,7 +3343,12 @@ namespace rr
33383343
template <> struct PrintValue::Ty<UInt>
33393344
{
33403345
static constexpr const char* fmt = "%u";
3341-
static std::vector<Value*> val(const RValue<UInt>& v) { return {v.value}; }
3346+
static std::vector<Value*> val(const RValue<UInt>& v);
3347+
};
3348+
template <> struct PrintValue::Ty<UInt2>
3349+
{
3350+
static constexpr const char* fmt = "[%u, %u]";
3351+
static std::vector<Value*> val(const RValue<UInt2>& v);
33423352
};
33433353
template <> struct PrintValue::Ty<UInt4>
33443354
{

0 commit comments

Comments
 (0)