Teach DataLayout that alignments on basic types must be powers of two.
authorOwen Anderson <resistor@mac.com>
Mon, 2 Mar 2015 09:35:03 +0000 (09:35 +0000)
committerOwen Anderson <resistor@mac.com>
Mon, 2 Mar 2015 09:35:03 +0000 (09:35 +0000)
Fixes assertion failures/crashes on bad datalayout specifications.

llvm-svn: 230940

llvm/lib/IR/DataLayout.cpp
llvm/test/Assembler/invalid-datalayout23.ll [new file with mode: 0644]
llvm/test/Assembler/invalid-datalayout24.ll [new file with mode: 0644]

index b6eaf6f..573e904 100644 (file)
@@ -405,6 +405,10 @@ DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
     report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
   if (!isUInt<16>(pref_align))
     report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
+  if (abi_align != 0 && !isPowerOf2_64(abi_align))
+    report_fatal_error("Invalid ABI alignment, must be a power of 2");
+  if (pref_align != 0 && !isPowerOf2_64(pref_align))
+    report_fatal_error("Invalid preferred alignment, must be a power of 2");
 
   if (pref_align < abi_align)
     report_fatal_error(
diff --git a/llvm/test/Assembler/invalid-datalayout23.ll b/llvm/test/Assembler/invalid-datalayout23.ll
new file mode 100644 (file)
index 0000000..4303263
--- /dev/null
@@ -0,0 +1,6 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+target datalayout = "i32:24:32"
+
+; CHECK: Invalid ABI alignment, must be a power of 2
+
diff --git a/llvm/test/Assembler/invalid-datalayout24.ll b/llvm/test/Assembler/invalid-datalayout24.ll
new file mode 100644 (file)
index 0000000..616ec64
--- /dev/null
@@ -0,0 +1,6 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+target datalayout = "i32:32:24"
+
+; CHECK: Invalid preferred alignment, must be a power of 2
+