froydnj opened Issue #1404:
Feature
lucetcneeds some way of accessing the trap sites for every function it compiles:In the above snippet, the trap site access happens after
Module::finishhas been called, so we're working withfaerieinterfaces.We are trying to port
lucetcto useobjectinstead offaerieunderneath, mostly solucetccan start generating PE/COFF objects with ease. It would reduce risk somewhat to be able to usecranelift-moduleas a high-level interface, swap betweencranelift-objectandcranelift-faeriewith as few lines of code as possible, and deal withobjectandfaeriethemselves as little as possible (probably just to write out the .o file). Therefore, it would be convenient if this information about traps associated with each function were accessible throughcranelift-moduleinterfaces, rather than through lower-level interfaces.Benefit
Trap information would be more easily accessible prior to module finalization. Though I don't know of anybody else who needs this kind of information about besides
lucetcat this point.Implementation
This is the tricky bit, because the three in-tree implementations differ substantially in their approach to trap collection:
cranelift-faeriecollects essentially(string-name, Vec<TrapSite>)pairs.cranelift-objectcollects a mapping ofFuncIdtoVec<TrapSite>.cranelift-simplejitignores traps entirely.I don't think there is a single method that all three interfaces could easily implement to iterate over
(string-name, &Vec<TrapSite>)pairs. To implement such an interface,cranelift-objectwould need to have a way to map fromFuncIdtostring-name, and that's awkward to do, given thatobjectitself doesn't deal with symbol names as strings, but asVec<u8>.The alternative, iterating over
(FuncId, &Vec<TrapSite>)pairs, would require some kind of modification tocranelift-faerie. We could changecranelift-faerieto map fromstring-nametoFuncId:cranelift-faeriecould grow a separate table for storing that mapping, butcranelift-moduleis already storing that mapping, and requiringcranelift-faerieto store it seems like wasted effort. Alternatively, the waycranelift-faeriestores its traps could be modified to look more likecranelift-object.But then maybe the right thing to do is just moving trap collection into
cranelift-moduleitself?Module::newwould then grow a flag for indicating whether trap collection should be done.Moduleitself would probably contain aFuncIdtoVec<TrapSite>mapping similar tocranelift-object, and thenBackend::define_functionwould receiveOption<&mut Vec<TrapSite>>for potentially accumulating traps.Since
cranelift-simplejitdoesn't collect traps, it doesn't matter too much what interface gets decided on.I think I prefer moving trap collection into
cranelift-moduleitself, but modifyingcranelift-faerieto make it more likecranelift-objectand the implementing some sort of common iterator interface works, too.Alternatives
The alternative is to keep the status quo, and
lucetcwould need to write two sets of code for defining certain pieces of information: one that defines those pieces withfaerieand another that defines them withobject.
philipc commented on Issue #1404:
To implement such an interface, cranelift-object would need to have a way to map from FuncId to string-name
cranelift-modulealready has a map fromFuncIdtoModuleFunctionwhich includes a name and is accessible via theModuleNamespacethat is commonly passed to backend methods.Another alternative is to return the traps when defining functions, instead of storing them in the backend. So
lucetcwill need to build its trap tables as it goes, instead of doing it all at then end. I haven't looked deeply to see how nicely this would work out, but it seems that each function trap table gets its own independent symbol so I think it could be okay.
pchickey commented on Issue #1404:
Returning the traps when defining functions seems like a good way to divide the responsibilities into cranelift-module and lucet. That would mean changing the codegen model of cranelift-module to do it at definition, rather than at finalize, but I don't think that is necessarily a bad thing.
pchickey edited a comment on Issue #1404:
Returning the traps when defining functions seems like a good way to divide the responsibilities into cranelift-module and lucet. That would mean changing cranelift-module to perform codegen at definition time, rather than at finalize, but I don't think that is necessarily a bad thing.
froydnj commented on Issue #1404:
That would mean changing cranelift-module to perform codegen at definition time, rather than at finalize, but I don't think that is necessarily a bad thing.
Doesn't
cranelift-modulealready do this?cranelift-object'sdefine_functionsays:and
cranelift-faerie/cranelift-simplejitdo very similar things. I guess they don't have to, but the documentation oncranelift-module::define_functionsays "Define a function, producing the function body from the givenContext."
pchickey commented on Issue #1404:
Sorry, bad memory on my part, cranelift-module does do it that way, the system in lucet that preceeded it waited until the end.
alexcrichton transferred Issue #1404:
Feature
lucetcneeds some way of accessing the trap sites for every function it compiles:In the above snippet, the trap site access happens after
Module::finishhas been called, so we're working withfaerieinterfaces.We are trying to port
lucetcto useobjectinstead offaerieunderneath, mostly solucetccan start generating PE/COFF objects with ease. It would reduce risk somewhat to be able to usecranelift-moduleas a high-level interface, swap betweencranelift-objectandcranelift-faeriewith as few lines of code as possible, and deal withobjectandfaeriethemselves as little as possible (probably just to write out the .o file). Therefore, it would be convenient if this information about traps associated with each function were accessible throughcranelift-moduleinterfaces, rather than through lower-level interfaces.Benefit
Trap information would be more easily accessible prior to module finalization. Though I don't know of anybody else who needs this kind of information about besides
lucetcat this point.Implementation
This is the tricky bit, because the three in-tree implementations differ substantially in their approach to trap collection:
cranelift-faeriecollects essentially(string-name, Vec<TrapSite>)pairs.cranelift-objectcollects a mapping ofFuncIdtoVec<TrapSite>.cranelift-simplejitignores traps entirely.I don't think there is a single method that all three interfaces could easily implement to iterate over
(string-name, &Vec<TrapSite>)pairs. To implement such an interface,cranelift-objectwould need to have a way to map fromFuncIdtostring-name, and that's awkward to do, given thatobjectitself doesn't deal with symbol names as strings, but asVec<u8>.The alternative, iterating over
(FuncId, &Vec<TrapSite>)pairs, would require some kind of modification tocranelift-faerie. We could changecranelift-faerieto map fromstring-nametoFuncId:cranelift-faeriecould grow a separate table for storing that mapping, butcranelift-moduleis already storing that mapping, and requiringcranelift-faerieto store it seems like wasted effort. Alternatively, the waycranelift-faeriestores its traps could be modified to look more likecranelift-object.But then maybe the right thing to do is just moving trap collection into
cranelift-moduleitself?Module::newwould then grow a flag for indicating whether trap collection should be done.Moduleitself would probably contain aFuncIdtoVec<TrapSite>mapping similar tocranelift-object, and thenBackend::define_functionwould receiveOption<&mut Vec<TrapSite>>for potentially accumulating traps.Since
cranelift-simplejitdoesn't collect traps, it doesn't matter too much what interface gets decided on.I think I prefer moving trap collection into
cranelift-moduleitself, but modifyingcranelift-faerieto make it more likecranelift-objectand the implementing some sort of common iterator interface works, too.Alternatives
The alternative is to keep the status quo, and
lucetcwould need to write two sets of code for defining certain pieces of information: one that defines those pieces withfaerieand another that defines them withobject.
Last updated: Dec 06 2025 at 05:03 UTC