AMDGPU: Fix bug causing crash due to invalid opencl version metadata.

Differential Revision: https://reviews.llvm.org/D22526

llvm-svn: 276119
This commit is contained in:
Yaxun Liu 2016-07-20 14:38:06 +00:00
parent b084125dbf
commit 4b1d9f7f18
4 changed files with 39 additions and 9 deletions

View 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);
}
}
}
}

View File

@ -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 = !{}

View File

@ -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 = !{}

View File

@ -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}