Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pure foreign functions #1

Merged
merged 2 commits into from
Apr 23, 2024
Merged

Support pure foreign functions #1

merged 2 commits into from
Apr 23, 2024

Conversation

edsko
Copy link
Collaborator

@edsko edsko commented Apr 19, 2024

For IO functions such as

foreign import capi "test_cbits.h slow_add" c_slowAddIO :: CLong -> CLong -> IO CLong

we rename c_slowAddIO to c_slowAddIO_uninstrumented and generate

c_slowAddIO arg0 arg1 = do 
   Debug.Trace.traceEventIO "trace-foreign-calls: call c_slowAddIO (capi safe \"test_cbits.h slow_add\")"
   result <- c_slowAddIO_uninstrumented arg0 arg1
   Debug.Trace.traceEventIO "trace-foreign-calls: return c_slowAddIO"
   return result

For pure functions such as

foreign import capi "test_cbits.h slow_add" c_slowAddPure :: CLong -> CLong -> CLong

we generate

c_slowAddIO arg0 arg1 = GHC.IO.Unsafe.unsafePerformIO $ do
    Debug.Trace.traceEventIO "trace-foreign-calls: call c_answerPure (capi safe \"test_cbits.h answer\")"
    result <- GHC.IO.evaluate c_answerPure_uninstrumented
    Debug.Trace.traceEventIO "trace-foreign-calls: return c_answerPure"
    return result

Note the call to unsafePerformIO and evaluate.

@edsko edsko force-pushed the edsko/pure-functions branch 3 times, most recently from e86be9f to e227a40 Compare April 23, 2024 15:21
@edsko edsko marked this pull request as ready for review April 23, 2024 15:22
@edsko edsko merged commit 7b8af41 into main Apr 23, 2024
2 checks passed
@edsko edsko deleted the edsko/pure-functions branch April 23, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant