Skip to content

Commit e7d50f7

Browse files
authored
LazyEncodedString everywhere (#247)
* Massive update to support LazyEncodedString Everywhere * Not complete! * This compiles the interop and wrapper libraries only, no impacted libs are updated yet. * Completed changes with updates to consumers * Includes additional testing * More changes to support LazyEncodedString everywhere. * Updated doc comments on `DisposeMEssageMarshaller type for clarity * Attempting to work around problems in JIT on Windows x64. * Apparently the JIT is assuming a 32 bit relocation as the default. - See llvm/llvm-project#65641 * This adds support for JIT builder and, in particular, TargetMachineBuilder so that the CodeModel is explicit in children * Static usings are now explicit everywhere they are needed. - This removes static usings from the global usings. * Additional support for LazyEncodedString * Converted name of static class for LLVM-C interop `Error` for consistency * Added marshaling form of `LLVMCreateStringError()` * Added `LLVMErrorRef.CreateForNativeOut()` - Eliminates need for annoying IDisposable warning suppression in many cases. - This support out/return of native handle for reverse P/Invoke scenarios. * Added setting the triple to the module transform for Kaleidoscope - Ensures the triple is set and matches the JIT. * Added `MarshalGCHandle.TryGet<>()` to support more intuitive acquisition of a managed context in native callbacks. * Moved static factory for `ErrorInfo` to a constructor. - Static factory was used for out/return to native but that causes a lot of suppression of warnings for dispose so isn't used. Instead, most APIs will use the static methods on LLVMErrorRef to report any errors. If the callback includes calling through a delegate, then it might use the constructor to build the resulting `ErrorInfo` to pass to native code. * Updated version checks in interop code * Since native API surface changed to support this build the version bump is needed * Additional support for LazyEncodedString * Added LibLLVMAttributeAllowedOn_All to make testing easier * Changed signature of LibLLVMGetAttributeInfo to use an out param as the result instead of a raw pointer - Wrapper layout is identical to the interop form so a simple cast through a pointer is all that is needed to "project" * Doc comment updates/fixes * [Build Fix] Corrected test * Test file was removed locally and missed - commit only included staged files so this edit and replace got in the way. --------- Co-authored-by: smaillet <[email protected]>
1 parent aa6d4d5 commit e7d50f7

File tree

185 files changed

+6104
-2664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+6104
-2664
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ csharp_style_conditional_delegate_call = true:warning
3030
csharp_style_expression_bodied_accessors = true:suggestion
3131
csharp_style_expression_bodied_indexers = true:suggestion
3232
csharp_style_expression_bodied_methods = true:suggestion
33-
csharp_style_expression_bodied_operators = true:suggestion
33+
csharp_style_expression_bodied_operators = when_on_single_line:silent
3434
csharp_style_expression_bodied_properties = true:warning
3535
csharp_style_inlined_variable_declaration = true:warning
3636
csharp_style_pattern_matching_over_as_with_null_check = true:warning
@@ -89,6 +89,8 @@ csharp_prefer_simple_default_expression = true:error
8989
csharp_space_between_method_declaration_parameter_list_parentheses = true
9090
csharp_space_between_method_declaration_empty_parameter_list_parentheses = true
9191
dotnet_diagnostic.SA0001.severity = none
92+
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
93+
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent
9294

9395
# Analysis and refactoring rules for Ubiquity.NET
9496
# Description: Code analysis rules for Ubiquity.NET projects
@@ -1594,3 +1596,4 @@ dotnet_style_prefer_simplified_interpolation = true:suggestion
15941596
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
15951597
dotnet_style_namespace_match_folder = true:suggestion
15961598
dotnet_diagnostic.MSTEST0032.severity = none
1599+
dotnet_code_quality_unused_parameters = all:suggestion

IgnoredWords.dic

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ app
99
apps
1010
argkind
1111
ascii
12+
atomicrmw
1213
attrib
1314
Attribs
1415
AttributeSet
16+
Attrs
1517
baz
1618
binaryop
1719
binlogs
1820
binop
1921
bitcode
2022
Blinky
23+
blittable
2124
blockdiag
2225
blog
2326
bool
@@ -26,8 +29,13 @@ buildtransitive
2629
builtinop
2730
byref
2831
byval
32+
castable
2933
cibuild
3034
Cmp
35+
Comdat
36+
Comdats
37+
compat
38+
Concat
3139
Config
3240
const
3341
contentfiles
@@ -41,14 +49,17 @@ dllimport
4149
docfx
4250
docfxconsole
4351
dotnet
52+
endianess
4453
enum
4554
Enums
4655
env
4756
exe
4857
facepalm
58+
finalizers
4959
foo
5060
fullsrc
5161
func
62+
getelementptr
5263
gh
5364
github
5465
Globalization
@@ -64,15 +75,18 @@ Llilum
6475
llvm
6576
llvmversion
6677
LValue
78+
marshallers
6779
marshalling
6880
memcopy
6981
metadata
7082
Mips
7183
msbuild
7284
msg
7385
nav
86+
nint
7487
noinline
7588
nounwind
89+
nuint
7690
nullability
7791
Nullable
7892
optimizenone
@@ -84,25 +98,34 @@ Pre
8498
proj
8599
readonly
86100
refactor
101+
refcount
87102
repl
88103
repo
89104
RMW
90105
runtimes
91106
RValue
107+
sizeof
92108
src
109+
Stateful
93110
struct
94111
structs
95112
Subrange
113+
Sym
96114
telliam
97115
tl
98116
trx
99117
typdef
100118
Typedef
119+
typeof
101120
uid
121+
uint
102122
unaryop
123+
Undefine
103124
undiscoverable
125+
Unhandled
104126
uniqued
105127
uniqueing
128+
unmarshal
106129
userdefinedop
107130
Users
108131
usings
@@ -112,6 +135,8 @@ vcxproj
112135
versioned
113136
versioning
114137
Xchg
138+
Xor
115139
xref
140+
xxx
116141
yaml
117142
yyyy

docfx/templates/Ubiquity/readme.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
# Ubiquity DOCFX template
22
This template adds support to the syntax highlighting provided by [HightlightJS](https://highlightjs.readthedocs.io/en/latest/supported-languages.html).
3-
The languages added are for ANTLR (Which seems bizarre it isn't already covered given the
4-
esoteric nature of some of the supported languages...) and of course the `Kaleidoscope`
5-
language, which was made up entirely for the purposes of LLVM tutorials. (No surprise that one
6-
isn't supported) [Though it oddly IS supported directly in the [Local MarkDig
7-
based editor](https://github.com/MadsKristensen/MarkdownEditor2022) used to edit these files...
3+
The languages added are for ANTLR (Which seems bizarre it isn't already covered given
4+
the esoteric nature of some of the supported languages...) and of course the `Kaleidoscope`
5+
language, which was made up entirely for the purposes of LLVM tutorials. (No surprise that
6+
one isn't supported) [Though it oddly IS supported directly in the [Local MarkDig based
7+
editor](https://github.com/MadsKristensen/MarkdownEditor2022) used to edit these files...
88

9+
## Theming
10+
This template also updates the theme for SVG image backgrounds to improve readability
11+
of the parse diagrams. It also updates the HighlightJS classifiers theming to better
12+
show various parts of the ANTLR language.
13+
14+
The default theming is limited, especially for the ANTLR language, as it doesn't
15+
support distinction between function definitions and invocations. (Both have
16+
essentially the default text formatting so it's like none is applied). HighlightJS has
17+
rather limited "scopes" and mapping to them for a language like ANTLR4 Lex/Grammar is
18+
a challenge. It is NOT an imperative language (but it does generate to that) but the
19+
HighlightJS is mostly focused on those.

src/Interop/InteropTests/AttributeTests.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,41 @@ public class AttributeTests
2828
public void CustomAttributeTests( )
2929
{
3030
using LLVMContextRef ctx = LLVMContextCreate();
31-
LazyEncodedString name = new("custom");
32-
LazyEncodedString value = new("custom value");
31+
LazyEncodedString name = new("custom"u8);
32+
LazyEncodedString value = "custom value"u8;
3333

34-
LLVMAttributeRef attribValue;
35-
36-
using var memName = name.Pin();
37-
using var memValue = value.Pin();
38-
unsafe
39-
{
40-
attribValue = LLVMCreateStringAttribute(ctx, (byte*)memName.Pointer, (uint)name.NativeStrLen, (byte*)memValue.Pointer, (uint)value.NativeStrLen);
41-
}
34+
LLVMAttributeRef attribValue = LLVMCreateStringAttribute(ctx, name, value);
4235

4336
Assert.IsFalse(attribValue.IsNull);
4437

4538
// LibLLVM should support Custom attributes as well.
46-
LibLLVMAttributeInfo info;
4739
LazyEncodedString attribName = new("custom");
48-
using var mem = attribName.Pin();
49-
unsafe
50-
{
51-
using LLVMErrorRef errorRef = LibLLVMGetAttributeInfo((byte*)mem.Pointer, attribName.NativeStrLen, &info);
52-
errorRef.ThrowIfFailed();
53-
}
40+
using LLVMErrorRef errorRef = LibLLVMGetAttributeInfo(attribName, out LibLLVMAttributeInfo info);
41+
errorRef.ThrowIfFailed();
5442

5543
Assert.AreEqual(LibLLVMAttributeArgKind.LibLLVMAttributeArgKind_String, info.ArgKind);
5644
}
5745

5846
[TestMethod]
5947
public void AttributeListAttainable( )
6048
{
61-
size_t len = LibLLVMGetNumKnownAttribs();
49+
int len = checked((int)LibLLVMGetNumKnownAttribs());
6250
unsafe
6351
{
6452
byte** ppData = stackalloc byte*[len];
65-
using LLVMErrorRef errorRef = LibLLVMGetKnownAttributeNames(len, ppData);
53+
using LLVMErrorRef errorRef = LibLLVMGetKnownAttributeNames(ppData, (nuint)len);
6654
errorRef.ThrowIfFailed();
6755

6856
// make sure conversion is plausible.
6957
var bldr = ImmutableArray.CreateBuilder<LazyEncodedString>(len);
7058
for(int i=0; i < len; ++i)
7159
{
72-
bldr.Add(new(ppData[i]));
60+
// https://github.com/microsoft/testfx/issues/5543
61+
#pragma warning disable MSTEST0037 // Use proper 'Assert' methods
62+
Assert.IsTrue(ppData[i] is not null);
63+
#pragma warning restore MSTEST0037 // Use proper 'Assert' methods
64+
65+
bldr.Add( LazyEncodedString.FromUnmanaged(ppData[i])!);
7366
}
7467

7568
var names = bldr.ToImmutable();

src/Interop/InteropTests/DataLayoutTests.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// </copyright>
55
// -----------------------------------------------------------------------
66

7-
using System;
87
using System.Runtime.InteropServices;
98

109
using Microsoft.VisualStudio.TestTools.UnitTesting;
11-
using Ubiquity.NET.Llvm.Interop.ABI.libllvm_c;
10+
11+
using Ubiquity.NET.InteropHelpers;
1212

1313
using static Ubiquity.NET.Llvm.Interop.ABI.libllvm_c.DataLayoutBindings;
1414

@@ -20,20 +20,14 @@ public class DataLayoutTests
2020
[TestMethod]
2121
public void TestParseKnownBad( )
2222
{
23-
ReadOnlySpan<byte> utf8Span = "badlayout"u8;
24-
unsafe
23+
LazyEncodedString utf8Span = "badlayout"u8;
24+
using var errorRef = LibLLVMParseDataLayout(utf8Span, out LLVMTargetDataRef retVal);
25+
using(retVal)
2526
{
26-
fixed(byte* p = &MemoryMarshal.GetReference(utf8Span))
27-
{
28-
using var ErrorRef = LibLLVMParseDataLayout(p, utf8Span.Length, out LLVMTargetDataRef retVal);
29-
using(retVal)
30-
{
31-
Assert.IsTrue(retVal.IsInvalid);
32-
Assert.IsTrue(ErrorRef.Failed);
33-
string errMsg = ErrorRef.ToString();
34-
Assert.IsFalse(string.IsNullOrWhiteSpace(errMsg));
35-
}
36-
}
27+
Assert.IsTrue(retVal.IsInvalid);
28+
Assert.IsTrue(errorRef.Failed);
29+
string errMsg = errorRef.ToString();
30+
Assert.IsFalse(string.IsNullOrWhiteSpace(errMsg), "Failure should have an error message");
3731
}
3832
}
3933
}

src/Interop/InteropTests/DebugRecordTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,20 @@ public void DebugRecordEnumerationSucceeds( )
6363
using LLVMDIBuilderRef diBuilder = LLVMCreateDIBuilder(module);
6464
LLVMMetadataRef int32DiType = LLVMDIBuilderCreateBasicType(
6565
diBuilder,
66-
"int32_t",
67-
7,
66+
"int32_t"u8,
6867
32,
6968
(uint)LibLLVMDwarfAttributeEncoding.DW_ATE_signed,
7069
LLVMDIFlags.LLVMDIFlagPublic
7170
);
7271

73-
LLVMMetadataRef int32PtrDiType = LLVMDIBuilderCreatePointerType(diBuilder, int32DiType, 32, 0, 0, "int*", 4);
74-
LLVMMetadataRef emptyExpression = LLVMDIBuilderCreateExpression(diBuilder, [], 0);
72+
LLVMMetadataRef int32PtrDiType = LLVMDIBuilderCreatePointerType(diBuilder, int32DiType, 32, 0, 0, "int*"u8);
73+
LLVMMetadataRef emptyExpression = LLVMDIBuilderCreateExpression(diBuilder, []);
7574
LLVMMetadataRef diFuncType = LLVMDIBuilderCreateSubroutineType(diBuilder, default, [], 0, LLVMDIFlags.LLVMDIFlagPrivate);
7675
LLVMMetadataRef scope = LLVMDIBuilderCreateFunction(
7776
diBuilder,
7877
Scope: default,
79-
Name: "TestFunc", NameLen: 8,
80-
LinkageName: "TestFunc", LinkageNameLen: 8,
78+
Name: "TestFunc"u8,
79+
LinkageName: "_TestFunc"u8,
8180
File: default,
8281
LineNo: 0,
8382
Ty: diFuncType,

src/Interop/InteropTests/LibraryInitTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
using Ubiquity.NET.Llvm.Interop.ABI.libllvm_c;
1414

1515
using static Ubiquity.NET.Llvm.Interop.ABI.llvm_c.TargetMachine;
16+
using Ubiquity.NET.InteropHelpers;
1617

1718
namespace Ubiquity.NET.Llvm.Interop.UT
1819
{
1920
[TestClass]
2021
public class LibraryInitTests
2122
{
22-
// Since these test validates the initialize and dispose of the the native library the
23+
// Since these test validates the initialize and dispose of the native library the
2324
// native code is no longer usable in the process that runs this test. Therefore this
2425
// is set to use a distinct process. (Sadly, this doesn't work in the VS test explorer
2526
// so it is simply marked as "skipped" there. Command line dotnet test via the scripts
@@ -66,12 +67,13 @@ public void TestLibraryReInit( )
6667
// NOTE: There are multiple actual targets under the one registered target
6768
// The registration is more along the lines of a "family" of targets...
6869
Assert.AreEqual(4, targets.Length);
69-
string?[] targetNames = [ .. targets.Select(h=>LLVMGetTargetName(h)) ];
70+
LazyEncodedString?[] targetNames = [ .. targets.Select(h=>LLVMGetTargetName(h)) ];
7071
// order of names/targets is not guaranteed, so just test for presence via Contains().
71-
Assert.IsTrue(targetNames.Contains("thumbeb"));
72-
Assert.IsTrue(targetNames.Contains("thumb"));
73-
Assert.IsTrue(targetNames.Contains("armeb"));
74-
Assert.IsTrue(targetNames.Contains("arm"));
72+
// Also test that u8 literal is viable
73+
Assert.IsTrue(targetNames.Contains<LazyEncodedString?>("thumbeb"u8));
74+
Assert.IsTrue(targetNames.Contains<LazyEncodedString?>("thumb"));
75+
Assert.IsTrue(targetNames.Contains<LazyEncodedString?>("armeb"));
76+
Assert.IsTrue(targetNames.Contains<LazyEncodedString?>("arm"));
7577
}
7678

7779
// After dispose - "that's all she wrote", LLVM native libraries

0 commit comments

Comments
 (0)