Comments (10)
I was reading rust-lang/rust#28728 about some loops being optimized away in LLVM. One comment suggested adding unsafe { asm!("" :::: "volatile")}
to the loop to prevent its removal, and this seems to work:
loop {
for _ in 0..400000 {
unsafe { asm!("" :::: "volatile")}
}
unsafe { write_volatile(PORTB, out) }
out = out ^ 0xff;
}
from blink.
Hmm. Can you post the LLVM IR that your version of the program generates? The write_volatile
should be marking the operations in a similar fashion to the volatile
marker.
from blink.
I built with RUSTFLAGS="--emit=llvm-ir" XARGO_RUST_SRC=/home/jjorgens/dev/rust/avr/rust rustup run avr-toolchain xargo build --target avr-atmega328p --release
The IIR output is pretty short:
; ModuleID = 'blink.cgu-0.rs'
source_filename = "blink.cgu-0.rs"
target datalayout = "e-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"
target triple = "avr-unknown-unknown"
; Function Attrs: norecurse noreturn nounwind uwtable
define void @main() unnamed_addr #0 {
start:
store volatile i8 -1, i8* inttoptr (i16 36 to i8*), align 4
br label %bb1
bb1: ; preds = %bb1, %start
store volatile i8 -1, i8* inttoptr (i16 37 to i8*), align 1
store volatile i8 0, i8* inttoptr (i16 37 to i8*), align 1
br label %bb1
}
; Function Attrs: norecurse nounwind readnone uwtable
define void @rust_eh_personality({}* nocapture, {}* nocapture) unnamed_addr #1 {
start:
ret void
}
attributes #0 = { norecurse noreturn nounwind uwtable }
attributes #1 = { norecurse nounwind readnone uwtable }
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"PIE Level", i32 2}
from blink.
If I were inclined to quick fix without digging into the root of the problem, the diff might look like
diff --git a/src/main.rs b/src/main.rs
index 9e01951..3165c9a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
#![feature(lang_items, unwind_attributes)]
-
+#![feature(asm)]
#![no_std]
#![no_main]
@@ -28,7 +28,9 @@ pub extern fn main() {
/// A small busy loop.
fn small_delay() {
- for _ in 0..10000 { }
+ for _ in 0..400000 {
+ unsafe { asm!("" :::: "volatile")}
+ }
}
// These do not need to be in a module, but we group them here for clarity.
I changed 10000 to 400000 so I actually see the blink.
from blink.
Oh! I totally confused this with my own blink repo which uses timers instead of a busy loop. No wonder I was so confused.
from blink.
I think the root cause here is that I suggested that people start compiling in release mode, as opposed to the original debug mode. Of course the optimizer sees that loop and throws it away ๐However, we weren't able to build in debug mode due to the inherent code bloat. Hmm.
I don't think a busy loop is really the correct end solution, but if we want to prevent it from being removed, your solution is closest.
I think you can submit your change (moving the feature
with the others in #![feature(lang_items, unwind_attributes)]
) as a fix for now!
from blink.
from blink.
Busy loop probably isn't right, but it's a pretty common first step in embedded land as an instructional aid as far as i can tell.
from blink.
I've raised avr-rust/rust-legacy-fork#85 so that we can develop a proper delay library.
Now that #4 has been merged, can this issue be closed?
from blink.
Closing is fine. I have my blink.
from blink.
Related Issues (20)
- failed to read libcompiler_builtins/Cargo.toml HOT 4
- Cannot compile libcore HOT 10
- failes to upload on real hardware HOT 3
- /usr/bin/avr-ld: unrecognized option '--eh-frame-hdr' HOT 1
- Failing to build in debug mode HOT 2
- SIGSEGV when compiling on latest nightly HOT 11
- Remove "how to setup a cross compiler" in README.md
- Build resolves incorrect version of ruduino when cargo.lock cleaned HOT 1
- LLVM ERROR: Not supported instr: <MCInst 258 <MCOperand Reg:1> <MCOperand Imm:15> <MCOperand Reg:45>> HOT 7
- similar name exists in the module: `atmega328p` HOT 3
- Simplify cargo build invocation HOT 3
- blinking to fast HOT 3
- Does it work with dependencies? HOT 1
- error: "/usr/lib/rustlib/src/rust/Cargo.lock" does not exist HOT 2
- couldn't allocate input reg for constrain 'w' HOT 4
- cannot find macro `llvm_asm` in this scope HOT 10
- could not load config key `env.AVR_CPU_FREQUENCY_HZ` HOT 4
- Linker does not support -no-pie command line option.
- FYI: possible moving crate HOT 2
- LLVM ERROR: Expected a constant shift amount HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blink.