-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add f16
inline ASM support for RISC-V
#126530
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ use rustc_codegen_ssa::traits::*; | |
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_middle::ty::layout::TyAndLayout; | ||
use rustc_middle::{bug, span_bug, ty::Instance}; | ||
use rustc_span::{Pos, Span}; | ||
use rustc_span::{sym, Pos, Span, Symbol}; | ||
use rustc_target::abi::*; | ||
use rustc_target::asm::*; | ||
use tracing::debug; | ||
|
@@ -64,7 +64,7 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { | |
let mut layout = None; | ||
let ty = if let Some(ref place) = place { | ||
layout = Some(&place.layout); | ||
llvm_fixup_output_type(self.cx, reg.reg_class(), &place.layout) | ||
llvm_fixup_output_type(self.cx, reg.reg_class(), &place.layout, instance) | ||
} else if matches!( | ||
reg.reg_class(), | ||
InlineAsmRegClass::X86( | ||
|
@@ -112,7 +112,7 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { | |
// so we just use the type of the input. | ||
&in_value.layout | ||
}; | ||
let ty = llvm_fixup_output_type(self.cx, reg.reg_class(), layout); | ||
let ty = llvm_fixup_output_type(self.cx, reg.reg_class(), layout, instance); | ||
output_types.push(ty); | ||
op_idx.insert(idx, constraints.len()); | ||
let prefix = if late { "=" } else { "=&" }; | ||
|
@@ -127,8 +127,13 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { | |
for (idx, op) in operands.iter().enumerate() { | ||
match *op { | ||
InlineAsmOperandRef::In { reg, value } => { | ||
let llval = | ||
llvm_fixup_input(self, value.immediate(), reg.reg_class(), &value.layout); | ||
let llval = llvm_fixup_input( | ||
self, | ||
value.immediate(), | ||
reg.reg_class(), | ||
&value.layout, | ||
instance, | ||
); | ||
inputs.push(llval); | ||
op_idx.insert(idx, constraints.len()); | ||
constraints.push(reg_to_llvm(reg, Some(&value.layout))); | ||
|
@@ -139,6 +144,7 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { | |
in_value.immediate(), | ||
reg.reg_class(), | ||
&in_value.layout, | ||
instance, | ||
); | ||
inputs.push(value); | ||
|
||
|
@@ -341,7 +347,8 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { | |
} else { | ||
self.extract_value(result, op_idx[&idx] as u64) | ||
}; | ||
let value = llvm_fixup_output(self, value, reg.reg_class(), &place.layout); | ||
let value = | ||
llvm_fixup_output(self, value, reg.reg_class(), &place.layout, instance); | ||
OperandValue::Immediate(value).store(self, place); | ||
} | ||
} | ||
|
@@ -913,12 +920,22 @@ fn llvm_asm_scalar_type<'ll>(cx: &CodegenCx<'ll, '_>, scalar: Scalar) -> &'ll Ty | |
} | ||
} | ||
|
||
fn any_target_feature_enabled( | ||
cx: &CodegenCx<'_, '_>, | ||
instance: Instance<'_>, | ||
features: &[Symbol], | ||
) -> bool { | ||
let enabled = cx.tcx.asm_target_features(instance.def_id()); | ||
features.iter().any(|feat| enabled.contains(feat)) | ||
} | ||
|
||
/// Fix up an input value to work around LLVM bugs. | ||
fn llvm_fixup_input<'ll, 'tcx>( | ||
bx: &mut Builder<'_, 'll, 'tcx>, | ||
mut value: &'ll Value, | ||
reg: InlineAsmRegClass, | ||
layout: &TyAndLayout<'tcx>, | ||
instance: Instance<'_>, | ||
) -> &'ll Value { | ||
let dl = &bx.tcx.data_layout; | ||
match (reg, layout.abi) { | ||
|
@@ -1029,6 +1046,16 @@ fn llvm_fixup_input<'ll, 'tcx>( | |
_ => value, | ||
} | ||
} | ||
(InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg), Abi::Scalar(s)) | ||
if s.primitive() == Primitive::Float(Float::F16) | ||
&& !any_target_feature_enabled(bx, instance, &[sym::zfhmin, sym::zfh]) => | ||
{ | ||
// Smaller floats are always "NaN-boxed" inside larger floats on RISC-V. | ||
let value = bx.bitcast(value, bx.type_i16()); | ||
let value = bx.zext(value, bx.type_i32()); | ||
let value = bx.or(value, bx.const_u32(0xFFFF_0000)); | ||
bx.bitcast(value, bx.type_f32()) | ||
} | ||
_ => value, | ||
} | ||
} | ||
|
@@ -1039,6 +1066,7 @@ fn llvm_fixup_output<'ll, 'tcx>( | |
mut value: &'ll Value, | ||
reg: InlineAsmRegClass, | ||
layout: &TyAndLayout<'tcx>, | ||
instance: Instance<'_>, | ||
) -> &'ll Value { | ||
match (reg, layout.abi) { | ||
(InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg), Abi::Scalar(s)) => { | ||
|
@@ -1140,6 +1168,14 @@ fn llvm_fixup_output<'ll, 'tcx>( | |
_ => value, | ||
} | ||
} | ||
(InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg), Abi::Scalar(s)) | ||
if s.primitive() == Primitive::Float(Float::F16) | ||
&& !any_target_feature_enabled(bx, instance, &[sym::zfhmin, sym::zfh]) => | ||
{ | ||
let value = bx.bitcast(value, bx.type_i32()); | ||
let value = bx.trunc(value, bx.type_i16()); | ||
bx.bitcast(value, bx.type_f16()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} | ||
_ => value, | ||
} | ||
} | ||
|
@@ -1149,6 +1185,7 @@ fn llvm_fixup_output_type<'ll, 'tcx>( | |
cx: &CodegenCx<'ll, 'tcx>, | ||
reg: InlineAsmRegClass, | ||
layout: &TyAndLayout<'tcx>, | ||
instance: Instance<'_>, | ||
) -> &'ll Type { | ||
match (reg, layout.abi) { | ||
(InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg), Abi::Scalar(s)) => { | ||
|
@@ -1242,6 +1279,12 @@ fn llvm_fixup_output_type<'ll, 'tcx>( | |
_ => layout.llvm_type(cx), | ||
} | ||
} | ||
(InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg), Abi::Scalar(s)) | ||
if s.primitive() == Primitive::Float(Float::F16) | ||
&& !any_target_feature_enabled(cx, instance, &[sym::zfhmin, sym::zfh]) => | ||
{ | ||
cx.type_f32() | ||
} | ||
_ => layout.llvm_type(cx), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2054,6 +2054,8 @@ symbols! { | |
yes, | ||
yield_expr, | ||
ymm_reg, | ||
zfh, | ||
zfhmin, | ||
zmm_reg, | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does LLVM not support using
f16
directly here? I expect this would result in much better codegen.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not. Regardless, LLVM's codegen isn't very good around when NaN-boxing needs to occur at the moment anyway; for example, an identity function will needlessly re-NaN-box an argument that is already guaranteed to be NaN-boxed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reading of the LLVM code is that it will accept
f16
directly if the zfhmin feature is enabled for the current function. Can you usef16
directly in this case and only do the manual conversion to/from f32 if zfhmin is not available?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done