Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: add a `volatile` memory flag that every pass respects (#14999)

TastyCZfed40461017a
diff --git a/cranelift/codegen/src/alias_analysis.rs b/cranelift/codegen/src/alias_analysis.rs
index 6d789c636e..97968fd65f 100644
--- a/cranelift/codegen/src/alias_analysis.rs
+++ b/cranelift/codegen/src/alias_analysis.rs
@@ -69,6 +69,14 @@
//! observe every store, because the loop might never terminate and
//! reach the overwriting store.
//!
+//! None of these apply to `volatile` accesses (see `MemFlagsData::volatile`),
+//! which must happen exactly as written: a volatile load is never replaced by
+//! a known value, a volatile store is never removed as idempotent or dead, and
+//! neither one provides a known value for a later access. A volatile access
+//! also never makes another store dead. Beyond that, every volatile access is
+//! a fence here, so no known value is forwarded across it, and every store
+//! before it is observed by it.
+//!
//! Which store is the "last store" to a region is flow-sensitive, but
//! whether a store is ever observed is *not*: it is observed if there
//! is *any* path on which some instruction can observe it. We
@@ -81,7 +89,9 @@ use crate::{
cursor::{Cursor, CursorPosition, FuncCursor},
dominator_tree::DominatorTree,
flowgraph::ControlFlowGraph,
- inst_predicates::{inst_addr_offset_type, inst_store_data, visit_block_succs},
+ inst_predicates::{
+ inst_addr_offset_type, inst_store_data, is_volatile_access, visit_block_succs,
+ },
ir::{
AliasRegion, Block, Endianness, Function, Inst, Opcode, Type, Value, immediates::Offset32,
},
@@ -338,6 +348,14 @@ fn classify(func: &Function, inst: Inst) -> InstEffect {
return InstEffect::Fence;
}
+ // A volatile access must stay as written, in order with the accesses
+ // around it: treat it as a fence, which observes every earlier store (so
+ // that none of them is removed as dead across it) and forgets everything
+ // known about memory (so that nothing is forwarded across it).
+ if is_volatile_access(func, inst) {
+ return InstEffect::Fence;
+ }
+
// Explicitly trapping instructions (`trap`, `trapz`, `udiv`,
// `uadd_overflow_trap`, etc... but not loads/stores that can implicitly
// trap): allow store-to-load forwarding across these instructions, but do
@@ -1192,7 +1210,13 @@ impl<'a> AliasAnalysis<'a> {
#[cfg(debug_assertions)]
debug_assert_eq!(func.layout.inst_block(inst), Some(state.current_block));
- let result = if let Some((address, offset, ty)) = inst_addr_offset_type(func, inst) {
+ let result = if is_volatile_access(func, inst) {
+ // A volatile access is never removed, never replaced by a known
+ // value, and never the source of one: skip straight to updating the
+ // last-store state, where it acts as a fence.
+ trace!(" --> volatile access: leaving it alone");
+ OptResult::None
+ } else if let Some((address, offset, ty)) = inst_addr_offset_type(func, inst) {
let address = func.dfg.resolve_aliases(address);
let opcode = func.dfg.insts[inst].opcode();
@@ -1220,6 +1244,9 @@ impl<'a> AliasAnalysis<'a> {
// atomic store) is observable by other threads, so it
// can never be eliminated.
&& !has_memory_fence_semantics(func.dfg.insts[last_store].opcode())
+ // Nor can a volatile store, which must happen exactly as
+ // written.
+ && !is_volatile_access(func, last_store)
// `last_store` must really be a store that
// writes exactly the bytes this store
// overwrites (same region, address, offset,
@@ -1574,6 +1601,12 @@ fn fully_overwrites(
return false;
}
+ // A volatile store is never dead, and a volatile store never makes another
+ // store dead.
+ if is_volatile_access(func, maybe_dead) || is_volatile_access(func, overwriter) {
+ return false;
+ }
+
// Both must write the same number of bytes: e.g. `istore8` and `store`
// write different widths even when their value types are equal.
let overwriter_opcode = func.dfg.insts[overwriter].opcode();
diff --git a/cranelift/codegen/src/inst_predicates.rs b/cranelift/codegen/src/inst_predicates.rs
index 244385cd53..08e4191b6b 100644
--- a/cranelift/codegen/src/inst_predicates.rs
+++ b/cranelift/codegen/src/inst_predicates.rs
@@ -32,13 +32,27 @@ fn is_load_with_defined_trapping(
}
}
+/// Is the given instruction a `volatile` memory access?
+///
+/// Such an access must happen exactly as written: see
+/// [`ir::MemFlagsData::volatile`].
+pub fn is_volatile_access(func: &Function, inst: Inst) -> bool {
+ func.dfg.insts[inst]
+ .memflags_data(&func.dfg)
+ .is_some_and(|flags| flags.volatile())
+}
+
/// Does the given instruction have any side-effect that would preclude it from being removed when
/// its value is unused?
+///
+/// A `volatile` load has one: the access itself is the effect.
#[inline(always)]
fn has_side_effect(func: &Function, inst: Inst) -> bool {
let data = &func.dfg.insts[inst];
let opcode = data.opcode();
- trivially_has_side_effects(opcode) || is_load_with_defined_trapping(opcode, data, &func.dfg)
+ trivially_has_side_effects(opcode)
+ || is_load_with_defined_trapping(opcode, data, &func.dfg)
+ || (opcode.can_load() && is_volatile_access(func, inst))
}
/// Does the given instruction behave as a "pure" node with respect to
@@ -54,6 +68,9 @@ pub fn is_pure_for_egraph(func: &Function, inst: Inst) -> bool {
..
} => {
let flags = func.dfg.mem_flags[flags];
+ // `readonly` and `can_move` each imply not `volatile` (the
+ // verifier rejects the combination), so such a load is never a
+ // volatile access.
flags.readonly() && flags.notrap() && flags.can_move()
}
_ => false,
diff --git a/cranelift/codegen/src/ir/memflags.rs b/cranelift/codegen/src/ir/memflags.rs
index 60ca090d5e..184080311e 100644
--- a/cranelift/codegen/src/ir/memflags.rs
+++ b/cranelift/codegen/src/ir/memflags.rs
@@ -223,6 +223,7 @@ impl MemFlagsData {
self.with_endianness(Endianness::Big)
}
"can_move" => self.with_can_move(true),
+ "volatile" => self.with_volatile(true),
other => match TrapCode::from_str(other) {
Ok(code) => self.with_trap_code(Some(code)),
@@ -360,6 +361,35 @@ impl MemFlagsData {
self.flags = self.flags.with_readonly(enabled);
self
}
+
+ /// Test if the `volatile` flag is set.
+ ///
+ /// A volatile memory operation must be performed exactly as written, as in
+ /// C's and Rust's volatile accesses (memory-mapped I/O, memory another
+ /// agent watches): it is never removed, even when its result is unused or
+ /// it stores a value already in memory; never merged with another access
+ /// (no redundant-load elimination, store-to-load forwarding or dead-store
+ /// elimination); and never reordered with another volatile access.
+ /// Accesses that are not volatile may still move around it.
+ ///
+ /// `volatile` is mutually exclusive with both `can_move` and `readonly`:
+ /// valid flags satisfy `!(volatile && (can_move || readonly))`, which the
+ /// verifier enforces.
+ pub const fn volatile(self) -> bool {
+ self.flags.volatile()
+ }
+
+ /// Set or clear the `volatile` flag.
+ pub fn set_volatile(&mut self, enabled: bool) {
+ *self = self.with_volatile(enabled);
+ }
+
+ /// Set or clear the `volatile` flag, returning new flags.
+ pub const fn with_volatile(mut self, enabled: bool) -> Self {
+ self.flags = self.flags.with_volatile(enabled);
+ self
+ }
+
/// Get the trap code to report if this memory access traps.
///
/// A `None` trap code indicates that this memory access does not trap.
diff --git a/cranelift/codegen/src/machinst/mod.rs b/cranelift/codegen/src/machinst/mod.rs
index e321045449..341f5186cd 100644
--- a/cranelift/codegen/src/machinst/mod.rs
+++ b/cranelift/codegen/src/machinst/mod.rs
@@ -79,6 +79,10 @@ const BIT_LITTLE_ENDIAN: u16 = 1 << 2;
/// Load multi-byte values from memory in a big-endian format.
const BIT_BIG_ENDIAN: u16 = 1 << 3;
+/// A volatile access: it happens exactly as written (once, with its own width,
+/// in order with other volatile accesses), even when its result is unused.
+const BIT_VOLATILE: u16 = 1 << 4;
+
/// Trap code, if any, for this memory operation.
const MASK_TRAP_CODE: u16 = ((1 << TRAP_CODE_BITS) - 1) << TRAP_CODE_OFFSET;
const TRAP_CODE_BITS: u16 = 8;
@@ -101,7 +105,8 @@ pub struct MachMemFlags {
// - Bit 1: readonly
// - Bit 2: little-endian
// - Bit 3: big-endian
- // - Bits 4..6: unused
+ // - Bit 4: volatile
+ // - Bits 5..6: unused
// - Bits 7..14: trap code
// - Bit 15: can_move
bits: u16,
@@ -203,6 +208,16 @@ impl MachMemFlags {
self.with_bit(BIT_READONLY, enabled)
}
+ /// Test if the `volatile` flag is set.
+ pub const fn volatile(self) -> bool {
+ self.read_bit(BIT_VOLATILE)
+ }
+
+ /// Set or clear the `volatile` flag, returning new flags.
+ pub const fn with_volatile(self, enabled: bool) -> Self {
+ self.with_bit(BIT_VOLATILE, enabled)
+ }
+
/// Get the trap code to report if this memory access traps.
pub const fn trap_code(self) -> Option<TrapCode> {
let byte = ((self.bits & MASK_TRAP_CODE) >> TRAP_CODE_OFFSET) as u8;
@@ -240,6 +255,9 @@ impl fmt::Display for MachMemFlags {
if self.can_move() {
write!(f, " can_move")?;
}
+ if self.volatile() {
+ write!(f, " volatile")?;
+ }
if self.read_bit(BIT_BIG_ENDIAN) {
write!(f, " big")?;
}
diff --git a/cranelift/codegen/src/verifier/mod.rs b/cranelift/codegen/src/verifier/mod.rs
index 9a935f9d69..b82fd8d898 100644
--- a/cranelift/codegen/src/verifier/mod.rs
+++ b/cranelift/codegen/src/verifier/mod.rs
@@ -1870,6 +1870,16 @@ impl<'a> Verifier<'a> {
fn immediate_constraints(&self, inst: Inst, errors: &mut VerifierErrors) -> VerifierStepResult {
let inst_data = &self.func.dfg.insts[inst];
+ if let Some(flags) = inst_data.memflags_data(&self.func.dfg) {
+ if flags.volatile() && (flags.readonly() || flags.can_move()) {
+ return errors.fatal((
+ inst,
+ self.context(inst),
+ "A `volatile` memory access cannot have the `readonly` or `can_move` MemFlag",
+ ));
+ }
+ }
+
match *inst_data {
ir::InstructionData::Store { flags, .. } => {
if self.func.dfg.mem_flags[flags].readonly() {
diff --git a/cranelift/docs/ir.md b/cranelift/docs/ir.md
index b86edcacef..58d96056f7 100644
--- a/cranelift/docs/ir.md
+++ b/cranelift/docs/ir.md
@@ -443,6 +443,11 @@ optimizations.
| notrap | Memory is assumed to be [accessible]. |
| aligned | Trapping allowed for misaligned accesses. |
| readonly | The data at the specified address will not modified between when this function is called and exited. |
+| volatile | The access must happen exactly as written: it is never removed or merged with another access. |
+
+Unlike the other flags, `volatile` restricts optimizations rather than enabling
+them. It is meant for accesses with side effects, such as memory-mapped I/O,
+and cannot be combined with `readonly` or `can_move`.
When the `accessible` flag is set, the behavior is undefined if the memory
is not [accessible].
diff --git a/cranelift/filetests/filetests/alias/volatile.clif b/cranelift/filetests/filetests/alias/volatile.clif
new file mode 100644
index 0000000000..6ac644f546
--- /dev/null
+++ b/cranelift/filetests/filetests/alias/volatile.clif
@@ -0,0 +1,229 @@
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+;; Volatile accesses happen exactly as written. `notrap` would let every access
+;; below be removed or merged; `volatile` must keep each one.
+
+;; Two volatile stores to the same location: the first is not a dead store.
+function %volatile_store_twice(i64) {
+block0(v0: i64):
+ v1 = iconst.i32 1
+ v2 = iconst.i32 2
+ store notrap aligned volatile v1, v0
+ store notrap aligned volatile v2, v0
+ return
+}
+
+; function %volatile_store_twice(i64) fast {
+; block0(v0: i64):
+; v1 = iconst.i32 1
+; store notrap aligned volatile v1, v0 ; v1 = 1
+; v2 = iconst.i32 2
+; store notrap aligned volatile v2, v0 ; v2 = 2
+; return
+; }
+
+;; Two volatile loads of the same location: the second is not redundant.
+function %volatile_load_twice(i64) -> i32 {
+block0(v0: i64):
+ v1 = load.i32 notrap aligned volatile v0
+ v2 = load.i32 notrap aligned volatile v0
+ v3 = iadd v1, v2
+ return v3
+}
+
+; function %volatile_load_twice(i64) -> i32 fast {
+; block0(v0: i64):
+; v1 = load.i32 notrap aligned volatile v0
+; v2 = load.i32 notrap aligned volatile v0
+; v3 = iadd v1, v2
+; return v3
+; }
+
+;; A volatile load after a volatile store of the same location: no forwarding.
+function %volatile_store_then_load(i64) -> i32 {
+block0(v0: i64):
+ v1 = iconst.i32 7
+ store notrap aligned volatile v1, v0
+ v2 = load.i32 notrap aligned volatile v0
+ return v2
+}
+
+; function %volatile_store_then_load(i64) -> i32 fast {
+; block0(v0: i64):
+; v1 = iconst.i32 7
+; store notrap aligned volatile v1, v0 ; v1 = 7
+; v2 = load.i32 notrap aligned volatile v0
+; return v2
+; }
+
+;; Storing back what a volatile load read is not an idempotent store.
+function %volatile_idempotent_store(i64) {
+block0(v0: i64):
+ v1 = load.i32 notrap aligned volatile v0
+ store notrap aligned volatile v1, v0
+ return
+}
+
+; function %volatile_idempotent_store(i64) fast {
+; block0(v0: i64):
+; v1 = load.i32 notrap aligned volatile v0
+; store notrap aligned volatile v1, v0
+; return
+; }
+
+;; Storing the same value twice with volatile stores keeps both.
+function %volatile_same_value_twice(i64, i32) {
+block0(v0: i64, v1: i32):
+ store notrap aligned volatile v1, v0
+ store notrap aligned volatile v1, v0
+ return
+}
+
+; function %volatile_same_value_twice(i64, i32) fast {
+; block0(v0: i64, v1: i32):
+; store notrap aligned volatile v1, v0
+; store notrap aligned volatile v1, v0
+; return
+; }
+
+;; A volatile load whose value is unused is still performed.
+function %volatile_load_unused(i64) {
+block0(v0: i64):
+ v1 = load.i32 notrap aligned volatile v0
+ return
+}
+
+; function %volatile_load_unused(i64) fast {
+; block0(v0: i64):
+; v1 = load.i32 notrap aligned volatile v0
+; return
+; }
+
+;; A plain store is not dead when a volatile store overwrites it, and a plain
+;; load after a volatile store is not forwarded from it.
+function %plain_around_volatile(i64) -> i32 {
+block0(v0: i64):
+ v1 = iconst.i32 1
+ v2 = iconst.i32 2
+ store notrap aligned v1, v0
+ store notrap aligned volatile v2, v0
+ v3 = load.i32 notrap aligned v0
+ return v3
+}
+
+; function %plain_around_volatile(i64) -> i32 fast {
+; block0(v0: i64):
+; v1 = iconst.i32 1
+; store notrap aligned v1, v0 ; v1 = 1
+; v2 = iconst.i32 2
+; store notrap aligned volatile v2, v0 ; v2 = 2
+; v3 = load.i32 notrap aligned v0
+; return v3
+; }
+
+;; Nothing known about memory is forwarded across a volatile access.
+function %no_forwarding_across_volatile(i64, i64) -> i32 {
+block0(v0: i64, v1: i64):
+ v2 = iconst.i32 1
+ store notrap aligned v2, v0
+ v3 = load.i32 notrap aligned volatile v1
+ v4 = load.i32 notrap aligned v0
+ v5 = iadd v3, v4
+ return v5
+}
+
+; function %no_forwarding_across_volatile(i64, i64) -> i32 fast {
+; block0(v0: i64, v1: i64):
+; v2 = iconst.i32 1
+; store notrap aligned v2, v0 ; v2 = 1
+; v3 = load.i32 notrap aligned volatile v1
+; v4 = load.i32 notrap aligned v0
+; v5 = iadd v3, v4
+; return v5
+; }
+
+;; A plain store before a volatile access is observed by it and stays, even
+;; when a later plain store overwrites it.
+function %plain_store_observed_by_volatile(i64, i64) {
+block0(v0: i64, v1: i64):
+ v2 = iconst.i32 1
+ v3 = iconst.i32 2
+ store notrap aligned v2, v0
+ v4 = load.i32 notrap aligned volatile v1
+ store notrap aligned v3, v0
+ return
+}
+
+; function %plain_store_observed_by_volatile(i64, i64) fast {
+; block0(v0: i64, v1: i64):
+; v2 = iconst.i32 1
+; store notrap aligned v2, v0 ; v2 = 1
+; v4 = load.i32 notrap aligned volatile v1
+; v3 = iconst.i32 2
+; store notrap aligned v3, v0 ; v3 = 2
+; return
+; }
+
+;; Volatile accesses in alias regions behave the same.
+function %volatile_in_region(i64) -> i32 {
+ region0 = 0 "mmio"
+block0(v0: i64):
+ v1 = iconst.i32 1
+ store notrap aligned volatile region0 v1, v0
+ store notrap aligned volatile region0 v1, v0
+ v2 = load.i32 notrap aligned volatile region0 v0
+ v3 = load.i32 notrap aligned volatile region0 v0
+ v4 = iadd v2, v3
+ return v4
+}
+
+; function %volatile_in_region(i64) -> i32 fast {
+; region0 = 0 "mmio"
+;
+; block0(v0: i64):
+; v1 = iconst.i32 1
+; store notrap aligned volatile region0 v1, v0 ; v1 = 1
+; store notrap aligned volatile region0 v1, v0 ; v1 = 1
+; v2 = load.i32 notrap aligned volatile region0 v0
+; v3 = load.i32 notrap aligned volatile region0 v0
+; v4 = iadd v2, v3
+; return v4
+; }
+
+;; Narrow, wide and vector volatile accesses, and extending loads.
+function %volatile_widths(i64) -> i8, i64, i128, i8x16, i32 {
+block0(v0: i64):
+ v1 = load.i8 notrap volatile v0
+ v2 = load.i8 notrap volatile v0
+ v3 = load.i64 notrap volatile v0
+ v4 = load.i64 notrap volatile v0
+ v5 = load.i128 notrap volatile v0
+ v6 = load.i128 notrap volatile v0
+ v7 = load.i8x16 notrap volatile v0
+ v8 = load.i8x16 notrap volatile v0
+ v9 = uload8.i32 notrap volatile v0
+ v10 = uload8.i32 notrap volatile v0
+ istore8 notrap volatile v9, v0
+ istore8 notrap volatile v10, v0
+ return v2, v4, v6, v8, v10
+}
+
+; function %volatile_widths(i64) -> i8, i64, i128, i8x16, i32 fast {
+; block0(v0: i64):
+; v1 = load.i8 notrap volatile v0
+; v2 = load.i8 notrap volatile v0
+; v3 = load.i64 notrap volatile v0
+; v4 = load.i64 notrap volatile v0
+; v5 = load.i128 notrap volatile v0
+; v6 = load.i128 notrap volatile v0
+; v7 = load.i8x16 notrap volatile v0
+; v8 = load.i8x16 notrap volatile v0
+; v9 = uload8.i32 notrap volatile v0
+; v10 = uload8.i32 notrap volatile v0
+; istore8 notrap volatile v9, v0
+; istore8 notrap volatile v10, v0
+; return v2, v4, v6, v8, v10
+; }
+
diff --git a/cranelift/filetests/filetests/egraph/volatile.clif b/cranelift/filetests/filetests/egraph/volatile.clif
new file mode 100644
index 0000000000..68b8307cf7
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/volatile.clif
@@ -0,0 +1,95 @@
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+;; A volatile load is a side effect: it is neither merged with an identical
+;; load in a dominated block (GVN) nor hoisted out of a loop (LICM).
+
+function %volatile_no_gvn(i64, i8) -> i32 {
+block0(v0: i64, v1: i8):
+ v2 = load.i32 notrap aligned volatile v0
+ brif v1, block1, block2
+
+block1:
+ v3 = load.i32 notrap aligned volatile v0
+ v4 = iadd v2, v3
+ return v4
+
+block2:
+ return v2
+}
+
+; function %volatile_no_gvn(i64, i8) -> i32 fast {
+; block0(v0: i64, v1: i8):
+; v2 = load.i32 notrap aligned volatile v0
+; brif v1, block1, block2
+;
+; block1:
+; v3 = load.i32 notrap aligned volatile v0
+; v4 = iadd.i32 v2, v3
+; return v4
+;
+; block2:
+; return v2
+; }
+
+function %volatile_no_licm(i64, i32) -> i32 {
+block0(v0: i64, v1: i32):
+ v2 = iconst.i32 0
+ jump block1(v2, v2)
+
+block1(v3: i32, v4: i32):
+ v5 = load.i32 notrap aligned volatile v0
+ v6 = iadd v4, v5
+ v9 = iconst.i32 1
+ v7 = iadd v3, v9
+ v8 = icmp ult v7, v1
+ brif v8, block1(v7, v6), block2
+
+block2:
+ return v6
+}
+
+; function %volatile_no_licm(i64, i32) -> i32 fast {
+; block0(v0: i64, v1: i32):
+; v2 = iconst.i32 0
+; v9 = iconst.i32 1
+; jump block1(v2, v2) ; v2 = 0, v2 = 0
+;
+; block1(v3: i32, v4: i32):
+; v5 = load.i32 notrap aligned volatile v0
+; v10 = iconst.i32 1
+; v11 = iadd v3, v10 ; v10 = 1
+; v8 = icmp ult v11, v1
+; v6 = iadd v4, v5
+; brif v8, block1(v11, v6), block2
+;
+; block2:
+; return v6
+; }
+
+;; Readonly, notrap and can_move loads are pure and merged; the same load
+;; with `volatile` (and without the flags `volatile` forbids) is not.
+function %pure_vs_volatile(i64) -> i32 {
+block0(v0: i64):
+ v1 = load.i32 notrap aligned readonly can_move v0
+ v2 = load.i32 notrap aligned readonly can_move v0
+ v3 = load.i32 notrap aligned volatile v0
+ v4 = load.i32 notrap aligned volatile v0
+ v5 = iadd v1, v2
+ v6 = iadd v3, v4
+ v7 = iadd v5, v6
+ return v7
+}
+
+; function %pure_vs_volatile(i64) -> i32 fast {
+; block0(v0: i64):
+; v3 = load.i32 notrap aligned volatile v0
+; v4 = load.i32 notrap aligned volatile v0
+; v1 = load.i32 notrap aligned readonly can_move v0
+; v5 = iadd v1, v1
+; v6 = iadd v3, v4
+; v7 = iadd v5, v6
+; return v7
+; }
+
diff --git a/cranelift/filetests/filetests/isa/x64/volatile.clif b/cranelift/filetests/filetests/isa/x64/volatile.clif
new file mode 100644
index 0000000000..a39ebd6123
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/x64/volatile.clif
@@ -0,0 +1,62 @@
+test compile precise-output
+target x86_64
+
+;; A volatile load may be folded into its user's memory operand like any
+;; other singly-used load: it is still performed once, with its own width.
+function %volatile_add(i64, i32) -> i32 {
+block0(v0: i64, v1: i32):
+ v2 = load.i32 notrap volatile v0
+ v3 = iadd.i32 v2, v1
+ return v3
+}
+
+; VCode:
+; pushq %rbp
+; movq %rsp, %rbp
+; block0:
+; movq %rsi, %rax
+; addl (%rdi), %eax
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+;
+; Disassembled:
+; block0: ; offset 0x0
+; pushq %rbp
+; movq %rsp, %rbp
+; block1: ; offset 0x4
+; movq %rsi, %rax
+; addl (%rdi), %eax
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+
+;; A volatile load is emitted even when its value is unused.
+function %volatile_load_unused(i64) {
+block0(v0: i64):
+ v1 = load.i64 notrap volatile v0
+ v2 = load.i8 notrap volatile v0
+ return
+}
+
+; VCode:
+; pushq %rbp
+; movq %rsp, %rbp
+; block0:
+; movq (%rdi), %rsi
+; movzbq (%rdi), %rsi
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+;
+; Disassembled:
+; block0: ; offset 0x0
+; pushq %rbp
+; movq %rsp, %rbp
+; block1: ; offset 0x4
+; movq (%rdi), %rsi
+; movzbq (%rdi), %rsi
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+
diff --git a/cranelift/filetests/filetests/verifier/volatile.clif b/cranelift/filetests/filetests/verifier/volatile.clif
new file mode 100644
index 0000000000..f27763540d
--- /dev/null
+++ b/cranelift/filetests/filetests/verifier/volatile.clif
@@ -0,0 +1,20 @@
+test verifier
+
+function %volatile_readonly(i64) -> i32 {
+block0(v0: i64):
+ v1 = load.i32 notrap readonly volatile v0 ; error: A `volatile` memory access cannot have the `readonly` or `can_move` MemFlag
+ return v1
+}
+
+function %volatile_can_move(i64) -> i32 {
+block0(v0: i64):
+ v1 = load.i32 notrap can_move volatile v0 ; error: A `volatile` memory access cannot have the `readonly` or `can_move` MemFlag
+ return v1
+}
+
+function %volatile_ok(i64, i32) -> i32 {
+block0(v0: i64, v1: i32):
+ store volatile v1, v0
+ v2 = load.i32 notrap volatile v0
+ return v2
+}