Translator: a legacy memory intrinsic's alignment that is not a power of two is a malformed record

Decode upgrades a call of the older LLVM's llvm.memcpy, llvm.memmove or llvm.memset (an i32
alignment before the volatile flag) by moving the alignment to the pointers' align. A constant
alignment that is not a power of two (or is above 2^32) was refused as
Intrinsic(ImmediateNotConstant), whose own doc says the operand is not a constant. It is now
Bitcode(MalformedRecord { block: FUNCTION_BLOCK, code: FUNC_CALL }), the decoder's answer to an
align attribute of such a value (attrs.rs). An alignment that is not a constant stays
Intrinsic(ImmediateNotConstant) at the call.

How: upgrade_legacy_memory returns a crate-private LegacyAlignmentError, Intrinsic(e) for the
immediate read's error and Malformed for a value Align::from_bytes refuses; the call's decode maps
Malformed to the CALL record's malformed-record error and Intrinsic(e) to the input error at the
record, as before. No public type changes; the outcome class is InvalidInput either way.

Makes a_legacy_memory_intrinsic_s_alignment_must_be_a_constant_power_of_two pass (expected; not
run: the brief holds every build and test until PR 30 merges). No SEMANTICS_VERSION bump here:
PR 30's 2391 covers the decode change, and only the error kind of an input refused either way
changes. Checked: cargo fmt clean.
This commit is contained in:
Anees Iqbal committed 2026-12-04 21:17:49 -08:00
1 parent 50b914e42b
commit f32b143935
2 files changed
+28 -10

No files matched your search

@@ -77,7 +77,7 @@ use super::ModuleValue;
use crate::budget::Meter;
use crate::error::{BitcodeError, DiagName, InputError, InputErrorKind, IntrinsicError, Location};
use crate::error::{Error, Feature, Limit};
use crate::intrinsics::{self, Bound, DeclClass};
use crate::intrinsics::{self, Bound, DeclClass, LegacyAlignmentError};
use crate::ir::{
AddrSpace, Align, Arena, Atomic, AtomicRmwOp, Block, BlockId, Body, Call, CallAttrs, Callee,
CastOp, Const, ConstId, ConstTable, FastMath, FloatPredicate, FuncId, Function, Inst, InstId,
@@ -1561,9 +1561,16 @@ impl FnDecoder {
};
if let Target::Intrinsic(_, class) = &target {
// An older LLVM's memory intrinsic takes its alignment as an operand; the current
// form states it as the pointers' `align` attribute, which lowering reads.
// form states it as the pointers' `align` attribute, which lowering reads. A constant
// alignment that is not a power of two is a malformed record, as an `align`
// attribute of that value is.
let upgraded = intrinsics::upgrade_legacy_memory(*class, &mut args, cx.consts)
.map_err(|e| InputError::new(InputErrorKind::Intrinsic(e), s.location()))?;
.map_err(|e| match e {
LegacyAlignmentError::Intrinsic(e) => {
Error::from(InputError::new(InputErrorKind::Intrinsic(e), s.location()))
}
LegacyAlignmentError::Malformed => bad(),
})?;
if let Some(legacy) = upgraded {
attrs = upgrade_legacy_attrs(attrs, &mut enums, legacy, m)?;
}
+18 -7
View File
@@ -578,19 +578,29 @@ pub(crate) struct LegacyAlignment {
pub pointers: usize,
}
/// Why a legacy memory intrinsic's call is not upgraded.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum LegacyAlignmentError {
/// The alignment is not a constant ([`IntrinsicError::ImmediateNotConstant`]).
Intrinsic(IntrinsicError),
/// The alignment is a constant that is not a power of two, or is above 2^32: a malformed
/// record, as an `align` attribute of that value is.
Malformed,
}
/// Upgrade a call of a memory intrinsic declared with an `i32` alignment before the volatile flag
/// (`llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1)`) to the current form, as LLVM's reader
/// upgrades it (`UpgradeIntrinsicCall`): the alignment operand is removed from `args` and returned,
/// and the caller removes that position's attributes and sets the pointer operands' `align` to it
/// (`setDestAlignment`, `setSourceAlignment`: replacing any the call states, and removing it for
/// an alignment of 0). `Ok(None)` for any other call. An alignment that is not a constant is
/// [`IntrinsicError::ImmediateNotConstant`], and so is a malformed one (not a power of two, or
/// above 2^32), which LLVM's verifier refuses: the error names an immediate the call cannot have.
/// [`IntrinsicError::ImmediateNotConstant`], and a malformed one (not a power of two, or above
/// 2^32), which LLVM's verifier refuses, is [`LegacyAlignmentError::Malformed`].
pub(crate) fn upgrade_legacy_memory(
class: DeclClass,
args: &mut Vec<Value>,
consts: &ConstTable,
) -> Result<Option<LegacyAlignment>, IntrinsicError> {
) -> Result<Option<LegacyAlignment>, LegacyAlignmentError> {
let DeclClass::Llvm(op) = class else {
return Ok(None);
};
@@ -601,11 +611,12 @@ pub(crate) fn upgrade_legacy_memory(
return Ok(None);
};
let removed = args.len().saturating_sub(2);
let align = match Immediates::new(legacy, args, consts).get(removed)? {
let bytes = Immediates::new(legacy, args, consts)
.get(removed)
.map_err(LegacyAlignmentError::Intrinsic)?;
let align = match bytes {
0 => None,
bytes => {
Some(crate::ir::Align::from_bytes(bytes).ok_or(IntrinsicError::ImmediateNotConstant)?)
}
bytes => Some(crate::ir::Align::from_bytes(bytes).ok_or(LegacyAlignmentError::Malformed)?),
};
args.remove(removed);
let pointers = match op {