Is it generally dangerous to inline codes #83192
-
What are some examples of the danger of inline codes randomly? I've noticed that in F# when I was working with NativePtr.stackalloc, I keep getting Is it better to inline manually |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Yes, inlining a function that has stack allocation inside can cause the "Common Language Runtime detected an invalid program" error in C# .NET. Inlining is an optimization technique used by the .NET runtime to improve the performance of a program by replacing a function call with the actual code of the function. However, when a function that has stack allocation inside is inlined, it can cause issues with the stack frame layout and result in an invalid program. Stack allocation involves allocating memory on the stack for local variables in a function. When the function returns, the stack frame is popped, and the memory is released. If the function is inlined, the compiler generates code that assumes the function's stack frame layout, and if it changes due to inlining, it can result in an invalid program. Therefore, inlining a function that has stack allocation inside can cause issues with the stack frame layout, which can result in the "Common Language Runtime detected an invalid program" error in C# .NET. It is essential to be cautious while using inlining, and you should ensure that the function being inlined does not have stack allocation or uses it in a way that will not cause issues with the stack frame layout. |
Beta Was this translation helpful? Give feedback.
-
Worth noting that F#'s F#'s
This only works because of how F#'s Could you provide a more complete example showing the code involved and the corresponding failure? -- CC. @vzarytovskii |
Beta Was this translation helpful? Give feedback.
-
Duplicate of dotnet/fsharp#8083. Apparently the let foobar () =
let foo = 1n + NativeInterop.NativePtr.toNativeInt (NativeInterop.NativePtr.stackalloc<byte> 10)
0
[<EntryPoint>]
let main _ = foobar () |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Duplicate of dotnet/fsharp#8083. Apparently the
localloc
instruction requires only the buffer's length to be present on the evaluation stack. The following F# program fails both in Debug and Release.