From 9f54647bede2c1b1505f5769696e5f469b36549c Mon Sep 17 00:00:00 2001 From: Alexander Root Date: Fri, 18 Feb 2022 18:37:34 -0500 Subject: [PATCH 1/3] Disallow Type::narrow() and Type::widen() from producing bitwidths between 1 and 8 bits --- src/Type.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Type.h b/src/Type.h index 6e9feee8b613..ee3bd47bc039 100644 --- a/src/Type.h +++ b/src/Type.h @@ -353,14 +353,24 @@ struct Type { return Type(code(), bits(), new_lanes, handle_type); } - /** Return Type with the same type code and number of lanes, but with twice as many bits. */ + /** Return Type with the same type code and number of lanes, but with least twice as many bits. */ Type widen() const { - return with_bits(bits() * 2); + if (bits() == 1) { + // Widening a 1-bit type should produce an 8-bit type. + return with_bits(8); + } else { + return with_bits(bits() * 2); + } } - /** Return Type with the same type code and number of lanes, but with half as many bits. */ + /** Return Type with the same type code and number of lanes, but with at most half as many bits. */ Type narrow() const { - return with_bits(bits() / 2); + if (bits() == 8) { + // Narrowing an 8-bit type should produce a 1-bit type. + return with_bits(1); + } else { + return with_bits(bits() / 2); + } } /** Type to be printed when declaring handles of this type. */ From 37bd9676434629249fee56c2af3dd0cb660fde1d Mon Sep 17 00:00:00 2001 From: Alexander Root Date: Fri, 18 Feb 2022 22:26:14 -0500 Subject: [PATCH 2/3] Narrowing a 1-bit type should error --- src/Type.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Type.h b/src/Type.h index ee3bd47bc039..ed734ed3b8b7 100644 --- a/src/Type.h +++ b/src/Type.h @@ -365,6 +365,7 @@ struct Type { /** Return Type with the same type code and number of lanes, but with at most half as many bits. */ Type narrow() const { + internal_assert(bits() != 1) << "Attempting to narrow a 1-bit type\n"; if (bits() == 8) { // Narrowing an 8-bit type should produce a 1-bit type. return with_bits(1); From 34a6e7753d7f70f00d29cba1fc1ee69d9e2217a2 Mon Sep 17 00:00:00 2001 From: Alexander Root Date: Sat, 19 Feb 2022 10:56:57 -0500 Subject: [PATCH 3/3] fix comment --- src/Type.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Type.h b/src/Type.h index ed734ed3b8b7..f020a4ae2625 100644 --- a/src/Type.h +++ b/src/Type.h @@ -353,7 +353,7 @@ struct Type { return Type(code(), bits(), new_lanes, handle_type); } - /** Return Type with the same type code and number of lanes, but with least twice as many bits. */ + /** Return Type with the same type code and number of lanes, but with at least twice as many bits. */ Type widen() const { if (bits() == 1) { // Widening a 1-bit type should produce an 8-bit type.