diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp --- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp +++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp @@ -91,17 +91,28 @@ // creation will depend on the TM and the code generation flags on the // function that reside in TargetOptions. resetTargetOptions(F); - auto ABIName = Options.MCOptions.getABIName(); - if (const MDString *ModuleTargetABI = dyn_cast_or_null( + + // The taget-abi option should equal to target-abi module flag because + // the different ABI has its corresponding datalayout and IR alignment + // information. In other words, the IR parser need to have datalayout + // information before it gets the ABI info from module. It does not make + // sense to update the module's datalayout and alignment information after + // IR parsing, so we just report an error and require user to give a correct + // -target-abi option. + StringRef OptABIName = Options.MCOptions.getABIName(); + if (const MDString *MDModuleABI = dyn_cast_or_null( F.getParent()->getModuleFlag("target-abi"))) { - auto TargetABI = RISCVABI::getTargetABI(ABIName); - if (TargetABI != RISCVABI::ABI_Unknown && - ModuleTargetABI->getString() != ABIName) { - report_fatal_error("-target-abi option != target-abi module flag"); - } - ABIName = ModuleTargetABI->getString(); + StringRef ModuleABIName = MDModuleABI->getString(); + if (OptABIName.empty() && OptABIName != ModuleABIName) + report_fatal_error("ABI is empty in -target-abi option, but target-abi " + "module flag is " + + ModuleABIName + "."); + if (OptABIName != ModuleABIName) + report_fatal_error("Incompatilbe ABI in -target-abi option and " + "target-abi module flag."); } - I = std::make_unique(TargetTriple, CPU, TuneCPU, FS, ABIName, *this); + I = std::make_unique(TargetTriple, CPU, TuneCPU, FS, + OptABIName, *this); } return I.get(); } diff --git a/llvm/test/CodeGen/RISCV/module-target-abi.ll b/llvm/test/CodeGen/RISCV/module-target-abi.ll --- a/llvm/test/CodeGen/RISCV/module-target-abi.ll +++ b/llvm/test/CodeGen/RISCV/module-target-abi.ll @@ -1,20 +1,18 @@ -; RUN: llc -mtriple=riscv32 < %s 2>&1 \ -; RUN: | FileCheck -check-prefix=DEFAULT %s +; Need to specific -target-abi option although the rv32 default ABI is +; ilp32 which eqaul to target-abi module flag. +; RUN: not --crash llc -mtriple=riscv32 < %s 2>&1 \ +; RUN: | FileCheck -check-prefix=EMPTY %s ; RUN: llc -mtriple=riscv32 -target-abi ilp32 < %s 2>&1 \ -; RUN: | FileCheck -check-prefix=RV32IF-ILP32 %s +; RUN: | FileCheck -check-prefix=MATCH %s ; RUN: not --crash llc -mtriple=riscv32 -target-abi ilp32f < %s 2>&1 \ -; RUN: | FileCheck -check-prefix=RV32IF-ILP32F %s -; RUN: llc -mtriple=riscv32 -filetype=obj < %s | llvm-readelf -h - | FileCheck -check-prefixes=FLAGS %s +; RUN: | FileCheck -check-prefix=MISMATCH %s -; RV32IF-ILP32F: -target-abi option != target-abi module flag - -; FLAGS: Flags: 0x0 +; EMPTY: LLVM ERROR: ABI is empty in -target-abi option, but target-abi module flag is ilp32. +; MISMATCH: LLVM ERROR: Incompatilbe ABI in -target-abi option and target-abi module flag. define float @foo(i32 %a) nounwind #0 { -; DEFAULT: # %bb.0: -; DEFAULT: fmv.x.w a0, ft0 -; RV32IF-ILP32: # %bb.0: -; RV32IF-ILP32: fmv.x.w a0, ft0 +; MATCH: # %bb.0: +; MATCH: fmv.x.w a0, ft0 %conv = sitofp i32 %a to float ret float %conv } diff --git a/llvm/test/CodeGen/RISCV/module-target-abi2.ll b/llvm/test/CodeGen/RISCV/module-target-abi2.ll --- a/llvm/test/CodeGen/RISCV/module-target-abi2.ll +++ b/llvm/test/CodeGen/RISCV/module-target-abi2.ll @@ -1,23 +1,18 @@ -; RUN: llc -mtriple=riscv32 < %s 2>&1 \ -; RUN: | FileCheck -check-prefix=DEFAULT %s -; RUN: not --crash llc -mtriple=riscv32 -target-abi ilp32 < %s 2>&1 \ -; RUN: | FileCheck -check-prefix=RV32IF-ILP32 %s +; Need to specific -target-abi option because target-abi module flag is not empty +; RUN: not --crash llc -mtriple=riscv32 < %s 2>&1 \ +; RUN: | FileCheck -check-prefix=EMPTY %s ; RUN: llc -mtriple=riscv32 -target-abi ilp32f < %s 2>&1 \ -; RUN: | FileCheck -check-prefix=RV32IF-ILP32F %s -; RUN: llc -mtriple=riscv32 -filetype=obj < %s | llvm-readelf -h - | FileCheck -check-prefixes=FLAGS %s - -; RV32IF-ILP32: -target-abi option != target-abi module flag +; RUN: | FileCheck -check-prefix=MATCH %s +; RUN: not --crash llc -mtriple=riscv32 -target-abi ilp32 < %s 2>&1 \ +; RUN: | FileCheck -check-prefix=MISMATCH %s -; FLAGS: Flags: 0x0 -; // this should be "Flags :0x2, single-float ABI", it will be fixed later. +; EMPTY: LLVM ERROR: ABI is empty in -target-abi option, but target-abi module flag is ilp32f. +; MISMATCH: LLVM ERROR: Incompatilbe ABI in -target-abi option and target-abi module flag. define float @foo(i32 %a) nounwind #0 { -; DEFAULT: # %bb.0: -; DEFAULT-NEXT: fcvt.s.w fa0, a0 -; DEFAULT-NEXT: ret -; RV32IF-ILP32F: # %bb.0: -; RV32IF-ILP32F: fcvt.s.w fa0, a0 -; RV32IF-ILP32F: ret +; MATCH: # %bb.0: +; MATCH: fcvt.s.w fa0, a0 +; MATCH: ret %conv = sitofp i32 %a to float ret float %conv }