AMDGPU: Fix bug causing crash due to invalid opencl version metadata.
authorYaxun Liu <Yaxun.Liu@amd.com>
Wed, 20 Jul 2016 14:38:06 +0000 (14:38 +0000)
committerYaxun Liu <Yaxun.Liu@amd.com>
Wed, 20 Jul 2016 14:38:06 +0000 (14:38 +0000)
Differential Revision: https://reviews.llvm.org/D22526

llvm-svn: 276119

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata1.ll [new file with mode: 0644]
llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata2.ll [new file with mode: 0644]
llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata3.ll [new file with mode: 0644]

index cfe6346..c9c95c7 100644 (file)
@@ -783,15 +783,19 @@ void AMDGPUAsmPrinter::emitStartOfRuntimeMetadata(const Module &M) {
   emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyMDVersion,
                         RuntimeMD::MDVersion << 8 | RuntimeMD::MDRevision, 2);
   if (auto MD = M.getNamedMetadata("opencl.ocl.version")) {
-    emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguage,
-                          RuntimeMD::OpenCL_C, 1);
-    auto Node = MD->getOperand(0);
-    unsigned short Major = mdconst::extract<ConstantInt>(Node->getOperand(0))
-                             ->getZExtValue();
-    unsigned short Minor = mdconst::extract<ConstantInt>(Node->getOperand(1))
-                             ->getZExtValue();
-    emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguageVersion,
-                          Major * 100 + Minor * 10, 2);
+    if (MD->getNumOperands()) {
+      auto Node = MD->getOperand(0);
+      if (Node->getNumOperands() > 1) {
+        emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguage,
+                              RuntimeMD::OpenCL_C, 1);
+        uint16_t Major = mdconst::extract<ConstantInt>(Node->getOperand(0))
+                         ->getZExtValue();
+        uint16_t Minor = mdconst::extract<ConstantInt>(Node->getOperand(1))
+                         ->getZExtValue();
+        emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguageVersion,
+                              Major * 100 + Minor * 10, 2);
+      }
+    }
   }
 }
 
diff --git a/llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata1.ll b/llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata1.ll
new file mode 100644 (file)
index 0000000..4e17a92
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: llc -mtriple=amdgcn--amdhsa < %s | FileCheck %s
+; check llc does not crash for invalid opencl version metadata
+
+; CHECK: .section        .AMDGPU.runtime_metadata
+; CHECK-NEXT: .byte    1
+; CHECK-NEXT: .short   256
+
+!opencl.ocl.version = !{}
diff --git a/llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata2.ll b/llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata2.ll
new file mode 100644 (file)
index 0000000..35b7d70
--- /dev/null
@@ -0,0 +1,9 @@
+; RUN: llc -mtriple=amdgcn--amdhsa < %s | FileCheck %s
+; check llc does not crash for invalid opencl version metadata
+
+; CHECK: .section        .AMDGPU.runtime_metadata
+; CHECK-NEXT: .byte    1
+; CHECK-NEXT: .short   256
+
+!opencl.ocl.version = !{!0}
+!0 = !{}
diff --git a/llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata3.ll b/llvm/test/CodeGen/AMDGPU/invalid-opencl-version-metadata3.ll
new file mode 100644 (file)
index 0000000..e169355
--- /dev/null
@@ -0,0 +1,9 @@
+; RUN: llc -mtriple=amdgcn--amdhsa < %s | FileCheck %s
+; check llc does not crash for invalid opencl version metadata
+
+; CHECK: .section        .AMDGPU.runtime_metadata
+; CHECK-NEXT: .byte    1
+; CHECK-NEXT: .short   256
+
+!opencl.ocl.version = !{!0}
+!0 = !{i32 1}