Comments (13)
I can test again on monday, I haven't really investigated in the builder situation yet.
from inkwell.
Thanks for your report!
Oh, yep, that's a problem. A problem we have here is that the LLVM api manipulates opaque pointers to start with, which means that using Rc
means reference-counting a reference in itself. It does appear to be one of the only solutions though.
from inkwell.
Great catch! I was unaware of this issue.
I think we should be able to hand out &Builder
references from a given Context
, which should be the simpler approach. However Module
s are far more complex, as ExecutionEngine
s need to take ownership of them. And indeed a ref counter may be the only solution like you stated.
I'll get to work on the Builder
fix ASAP (unless either of you would like to give it a PR). Module
warrants more research, I think.
from inkwell.
Would you mind providing some simple examples of how to reproduce this issue? They'll be super helpful as test cases. Thanks!
from inkwell.
Sure, the most simple test case would look something like this:
fn main() {
let module = {
let context = Context::create();
context.create_module("test")
};
// do something with module here
}
Currently, this code will trigger undefined behaviour (on my macbook it spits out "an unknown error occured").
from inkwell.
One library that also fixes this by using Arc
to the parent object is vulkano.
Look at this code for example: https://github.com/vulkano-rs/vulkano/blob/master/vulkano/src/device.rs#L126
You should consider using Arc
instead of Rc
to, if multithreading is something you want to support down the line (but I'm not sure how threadsafe LLVM is in general, so maybe this isn't a concern)
from inkwell.
One last note after thinking about this for a while.
Context handing out references to Builder is IMO not a good solution.
- It will require the context to hold a
Vec<Builder>
, which is a lot more costly than a simple Rc (requires allocation) - The user will probably end up in lifetime hell (You usually want to store the builder in a wrapping struct when implementing a custom compiler)
In my compiler for example, I have a struct that looks something like this:
pub struct LLVMBuilder {
ctx: Context,
module: Module,
builder: Builder,
}
Would this even be possible with &Builder, that is owned by the Context?
Even if it is, probably a pita to deal with.
from inkwell.
I believe LLVM does support multithreading, but it isn't on my radar to support anytime soon. I see it as a way advanced feature that will likely have its own set of complications. I did create a ticket for it here, though: #28 I think it should be straightforward to convert any Rc
types to Arc
That's a great point. Rc
probably allocates, though, no? Arc
would almost definitely do so. But anyway, I think I agree overall. I'll have to look into how Vulkano does it some more. Thanks!
ExecutionEngine
currently takes ownership of Modules
and deals out references to them, which makes it quite awkward to modify the EE. I wonder if it should follow this Rc
pattern, too.
from inkwell.
@nicokoch So I'm having trouble reproducing this for builder:
let builder = {
let context = Context::create();
context.create_builder()
};
builder.build_unreachable();
but there is no invalid memory access as far as I can tell. Tests pass fine (though I know that's not a perfect validation) Are you seeing otherwise on your system?
from inkwell.
For the builder it doesn't crash on my machine either.
Nonetheless, even if no invalid memory access occurs, it doesn't mean it cannot occur under any environment.
We are probably on the safe side implementing the fix for builder too.
That's a great point. Rc probably allocates, though, no?
Yes, Rc does allocate, my bad.
from inkwell.
Yeah, I agree but I would feel better about the builder fix if there was some way to verify it works.
from inkwell.
There is! Just add a println! inside the drop implementation and verify that Context's drop is always called after Builder's drop (for example with the code snippet from earlier)
from inkwell.
I'm not convinced builders are as tied to the hip to their context as modules are.
You suggested verifying the context dropped after builder:
let context = Context::create();
let builder = context.create_builder();
drop(builder);
drop(context);
but that did not yield any invalid memory access either.
For example using the builder with a context other than the one it was created with (and the original destroyed) works fine:
let builder = {
let context = Context::create();
context.create_builder()
// Original Context drops fine
};
// Builder continues to function with different context
let context = Context::create();
let module = context.create_module("my_mod");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let fn_value = module.add_function("my_fn", &fn_type, None);
let entry = fn_value.append_basic_block("entry");
builder.position_at_end(&entry);
builder.build_unreachable();
module.print_to_string();
// Prints: "; ModuleID = \'my_mod\'\n\ndefine void @my_fn() {\nentry:\n unreachable\n}\n");
// 2nd Context drops fine
// Builds drops fine
This is looking like a Module
only issue until we can prove otherwise. You are correct in stating it may be possible in some environment, but without having one as an example it's hard to verify.
from inkwell.
Related Issues (20)
- Couldn't compile HOT 5
- Unable to Access Context Dynamically in LLVMManager: Seeking Alternative Approach HOT 2
- `InstructionValue::new` is `pub(crate)` instead of `pub` HOT 2
- Segfaults when builder is called without setting its position first HOT 5
- Add llvm-sys linkage flags
- Question: How to use LLVM `type`? HOT 5
- More examples? HOT 6
- Question: Is there a way to get the BasicValueEnum from the name of the instruction. HOT 6
- Question: What does `get_insert_block` returns. HOT 2
- Question: How can I get all defined structures? HOT 2
- Question: How to get the size of a type during LLVM generation? HOT 4
- Add `typos` to CI
- Create new fine-grained enum for `build_cast` HOT 1
- How to link library to jit? HOT 2
- Support llvm 17 HOT 1
- Add support for `global_ctor` HOT 4
- Add support for `global_ctor` HOT 3
- ORC support HOT 1
- Const struct member access HOT 1
- Segfault when using `module.create_jit_execution_engine()` 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 inkwell.