Skip to content

Commit 398d8bb

Browse files
committed
Updated to new LLVM APIs
* Used call to `LLVMDIBuilderFinalizeSubprogram` instead of extension API. * Moved support of getting current Debug Metadata version to library interface * Added module property to get version of debug metadata that is present in a given module * Added support to module to strop debug information * Updated CodeGenWithDebugInfo sample to leverage debug version value for the library as a whole from the library
1 parent 4bafed5 commit 398d8bb

File tree

11 files changed

+51
-38
lines changed

11 files changed

+51
-38
lines changed

src/Samples/CodeGenWithDebugInfo/CortexM3ABI.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ namespace CodeGenWithDebugInfo
1818
internal sealed class CortexM3ABI
1919
: ITargetABI
2020
{
21-
public CortexM3ABI( )
21+
public CortexM3ABI( ILibLlvm library)
2222
{
23-
LlvmLib = Library.InitializeLLVM();
24-
LlvmLib.RegisterTarget( CodeGenTarget.ARM );
23+
library.RegisterTarget( CodeGenTarget.ARM );
2524
}
2625

2726
public string ShortName => "M3";
2827

29-
public void Dispose( )
30-
{
31-
LlvmLib.Dispose();
32-
}
33-
3428
public TargetMachine CreateTargetMachine( )
3529
{
3630
using var triple = new Triple( TripleName );
@@ -81,8 +75,8 @@ public void AddAttributesForByValueStructure( Function function, DebugFunctionTy
8175
public void AddModuleFlags( Module module )
8276
{
8377
// Specify ABI const sizes so linker can detect mismatches
84-
module.AddModuleFlag( ModuleFlagBehavior.Error, "wchar_size", 4 );
85-
module.AddModuleFlag( ModuleFlagBehavior.Error, "min_enum_size", 4 );
78+
module.AddModuleFlag( ModuleFlagBehavior.Error, "wchar_size"u8, 4 );
79+
module.AddModuleFlag( ModuleFlagBehavior.Error, "min_enum_size"u8, 4 );
8680
}
8781

8882
public ImmutableArray<AttributeValue> BuildTargetDependentFunctionAttributes( IContext ctx )
@@ -104,8 +98,6 @@ public ImmutableArray<AttributeValue> BuildTargetDependentFunctionAttributes( IC
10498
ctx.CreateAttribute( "use-soft-float"u8, "false"u8 )
10599
];
106100

107-
private readonly ILibLlvm LlvmLib;
108-
109101
// Sadly, these can't be utf8 literals, but, they can be static readonly LazyEncodedString!
110102
private static readonly LazyEncodedString Cpu = "cortex-m3"u8;
111103
private static readonly LazyEncodedString Features = "+hwdiv,+strict-align,+thumb-mode"u8;

src/Samples/CodeGenWithDebugInfo/ITargetABI.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace CodeGenWithDebugInfo
2525
/// </remarks>
2626
/// <seealso href="https://discourse.llvm.org/t/llvm-introduce-an-abi-lowering-library/84554"/>
2727
internal interface ITargetABI
28-
: IDisposable
2928
{
3029
string ShortName { get; }
3130

src/Samples/CodeGenWithDebugInfo/Program.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Ubiquity.NET.Llvm;
1414
using Ubiquity.NET.Llvm.DebugInfo;
1515
using Ubiquity.NET.Llvm.Instructions;
16-
1716
using Ubiquity.NET.Llvm.Types;
1817
using Ubiquity.NET.Llvm.Values;
1918

@@ -58,8 +57,10 @@ public static void Main( string[] args )
5857
srcPath = Path.GetFullPath( srcPath );
5958
#endregion
6059

60+
using ILibLlvm library = Library.InitializeLLVM();
61+
6162
#region TargetABISelection
62-
using var targetABI = AbiFactory(args[0]);
63+
ITargetABI? targetABI = AbiFactory(library, args[0]);
6364
if(targetABI is null)
6465
{
6566
ShowUsage();
@@ -126,7 +127,7 @@ public static void Main( string[] args )
126127
// add module flags and compiler identifiers...
127128
// this can technically occur at any point, though placing it here makes
128129
// comparing against clang generated files easier
129-
AddModuleFlags( targetABI, module );
130+
AddModuleFlags( targetABI, module, library );
130131
#endregion
131132

132133
#region CreatingQualifiedTypes
@@ -172,12 +173,12 @@ private static void ShowUsage( )
172173
Console.Error.WriteLine( "Usage: CodeGenWithDebugInfo [X64|M3] <source file path>" );
173174
}
174175

175-
private static ITargetABI? AbiFactory( string arg )
176+
private static ITargetABI? AbiFactory( ILibLlvm library, string arg )
176177
{
177178
return arg.ToUpperInvariant() switch
178179
{
179-
"M3" => new CortexM3ABI(),
180-
"X64" => new X64ABI(),
180+
"M3" => new CortexM3ABI(library),
181+
"X64" => new X64ABI(library),
181182
_ => null,
182183
};
183184
}
@@ -258,10 +259,10 @@ private static Function DeclareCopyFunc( ITargetABI abi
258259
#endregion
259260

260261
#region AddModuleFlags
261-
private static void AddModuleFlags( ITargetABI abi, Module module )
262+
private static void AddModuleFlags( ITargetABI abi, Module module, ILibLlvm library )
262263
{
263264
module.AddModuleFlag( ModuleFlagBehavior.Warning, Module.DwarfVersionValue, 4 );
264-
module.AddModuleFlag( ModuleFlagBehavior.Warning, Module.DebugVersionValue, Module.DebugMetadataVersion );
265+
module.AddModuleFlag( ModuleFlagBehavior.Warning, Module.DebugVersionValue, library.DebugMetadataVersion );
265266
abi.AddModuleFlags( module );
266267
module.AddVersionIdentMetadata( VersionIdentString );
267268
}

src/Samples/CodeGenWithDebugInfo/X64ABI.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ namespace CodeGenWithDebugInfo
1818
internal sealed class X64ABI
1919
: ITargetABI
2020
{
21-
public X64ABI( )
21+
public X64ABI( ILibLlvm library )
2222
{
23-
LlvmLib = Library.InitializeLLVM();
24-
LlvmLib.RegisterTarget( CodeGenTarget.X86 );
23+
library.RegisterTarget( CodeGenTarget.X86 );
2524
}
2625

2726
public string ShortName => "x86";
2827

29-
public void Dispose( )
30-
{
31-
LlvmLib.Dispose();
32-
}
33-
3428
public TargetMachine CreateTargetMachine( )
3529
{
3630
using var triple = new Triple( TripleName );
@@ -63,7 +57,7 @@ public void AddAttributesForByValueStructure( Function function, DebugFunctionTy
6357

6458
public void AddModuleFlags( Module module )
6559
{
66-
module.AddModuleFlag( ModuleFlagBehavior.Error, "PIC Level", 2 );
60+
module.AddModuleFlag( ModuleFlagBehavior.Error, "PIC Level"u8, 2 );
6761
}
6862

6963
public ImmutableArray<AttributeValue> BuildTargetDependentFunctionAttributes( IContext ctx )
@@ -81,8 +75,6 @@ public ImmutableArray<AttributeValue> BuildTargetDependentFunctionAttributes( IC
8175
ctx.CreateAttribute( "uwtable"u8, (ulong)UWTableKind.Async)
8276
];
8377

84-
private readonly ILibLlvm LlvmLib;
85-
8678
// Sadly, these can't be utf8 literals, but, they can be static readonly LazyEncodedString!
8779
private static readonly LazyEncodedString Cpu = "x86-64"u8;
8880
private static readonly LazyEncodedString Features = "+sse,+sse2"u8;

src/Ubiquity.NET.Llvm.Tests/ModuleTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,13 @@ public void GetTypeByNameTest( )
393393
[TestMethod]
394394
public void AddModuleFlagTest( )
395395
{
396+
Assert.IsNotNull(ModuleFixtures.LibLLVM);
397+
396398
using var context = new Context( );
397399
using var module = context.CreateBitcodeModule( TestModuleName );
398400

399401
module.AddModuleFlag( ModuleFlagBehavior.Warning, Module.DwarfVersionValue, 4 );
400-
module.AddModuleFlag( ModuleFlagBehavior.Warning, Module.DebugVersionValue, Module.DebugMetadataVersion );
402+
module.AddModuleFlag( ModuleFlagBehavior.Warning, Module.DebugVersionValue, ModuleFixtures.LibLLVM.DebugMetadataVersion );
401403
module.AddModuleFlag( ModuleFlagBehavior.Error, "wchar_size", 4 );
402404
module.AddModuleFlag( ModuleFlagBehavior.Error, "min_enum_size", 4 );
403405
module.AddVersionIdentMetadata( "unit-tests 1.0" );
@@ -424,7 +426,7 @@ public void AddModuleFlagTest( )
424426

425427
var debugVerConst = ( ( ConstantAsMetadata )debugVerFlag.Metadata ).Constant;
426428
Assert.IsInstanceOfType<ConstantInt>( debugVerConst );
427-
Assert.AreEqual( Module.DebugMetadataVersion, ((ConstantInt)debugVerConst).ZeroExtendedValue );
429+
Assert.AreEqual( ModuleFixtures.LibLLVM.DebugMetadataVersion, ((ConstantInt)debugVerConst).ZeroExtendedValue );
428430

429431
var wcharSizeFlag = module.ModuleFlags[ "wchar_size" ];
430432
Assert.AreEqual( ModuleFlagBehavior.Error, wcharSizeFlag.Behavior );

src/Ubiquity.NET.Llvm/DebugInfo/DIBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ public readonly DIGlobalVariableExpression CreateGlobalVariableExpression( DINod
10951095
public readonly void Finish( DISubProgram subProgram )
10961096
{
10971097
ArgumentNullException.ThrowIfNull( subProgram );
1098-
LibLLVMDIBuilderFinalizeSubProgram( Handle.ThrowIfInvalid(), subProgram.Handle );
1098+
LLVMDIBuilderFinalizeSubprogram( Handle.ThrowIfInvalid(), subProgram.Handle );
10991099
}
11001100

11011101
/// <summary>Finalizes debug information for all items built by this builder</summary>

src/Ubiquity.NET.Llvm/ILibLLVM.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,8 @@ public interface ILibLlvm
144144
/// attributes not yet known to, or considered stable by, the LLVM core native code.
145145
/// </remarks>
146146
ImmutableDictionary<LazyEncodedString, AttributeInfo> AttributeMap { get; }
147+
148+
/// <summary>Gets the current debug metadata version for this library</summary>
149+
uint DebugMetadataVersion { get; }
147150
}
148151
}

src/Ubiquity.NET.Llvm/IModule.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public interface IModule
8686
/// <summary>Gets or sets the module level inline assembly</summary>
8787
public LazyEncodedString ModuleInlineAsm { get; set; }
8888

89+
/// <summary>Gets the Debug metadata version that is present in this module</summary>
90+
public uint DebugMetadataVersion { get; }
91+
8992
/// <summary>Appends inline assembly to the module's inline assembly</summary>
9093
/// <param name="asm">assembly text</param>
9194
public void AppendInlineAsm( LazyEncodedString asm );
@@ -391,6 +394,10 @@ public Function CreateFunction( ref readonly DIBuilder diBuilder
391394
/// <param name="targetContext"><see cref="IContext"/> to clone the module into</param>
392395
/// <returns>Cloned copy of the module</returns>
393396
public Module Clone( IContext targetContext );
397+
398+
/// <summary>Strips debug information from this module</summary>
399+
/// <returns><see langword="true"/> if the module was modified; <see langword="false"/> if not</returns>
400+
public bool StripDebugInformation();
394401
}
395402

396403
// Internal helper to get the underlying ABI handle as an "Alias" handle

src/Ubiquity.NET.Llvm/Library.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ the low level interop (Test code sometimes does) it must explicitly reference it
1313
using System.Collections.Immutable;
1414

1515
using static Ubiquity.NET.Llvm.Interop.ABI.libllvm_c.AttributeBindings;
16+
using static Ubiquity.NET.Llvm.Interop.ABI.llvm_c.DebugInfo;
1617

1718
// Apply using aliases to simplify avoidance of name conflicts.
1819
using InteropCodeGenTarget = Ubiquity.NET.Llvm.Interop.ABI.libllvm_c.LibLLVMCodeGenTarget;
@@ -55,6 +56,9 @@ public static ILibLlvm InitializeLLVM( )
5556
/// <summary>Gets the native target for the current runtime</summary>
5657
public static CodeGenTarget NativeTarget => (CodeGenTarget)RuntimeInformation.ProcessArchitecture.AsLLVMTarget();
5758

59+
/// <inheritdoc/>
60+
public uint DebugMetadataVersion => LLVMDebugMetadataVersion();
61+
5862
// "MOVE" construction, this instance takes over responsibility
5963
// of calling dispose.
6064
internal Library( InteropItf impl )

src/Ubiquity.NET.Llvm/Module.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public override bool Equals( object? obj ) => obj is Module owner
7272
/// <summary>Name of the Dwarf Version module flag</summary>
7373
public static readonly LazyEncodedString DwarfVersionValue = "Dwarf Version"u8;
7474

75-
/// <summary>Gets the current Version of the Debug information used by LLVM</summary>
76-
public static UInt32 DebugMetadataVersion => LLVMDebugMetadataVersion();
77-
7875
/// <summary>Gets a value indicating whether this instance is already disposed</summary>
7976
public bool IsDisposed => NativeHandle is null || NativeHandle.IsInvalid || NativeHandle.IsClosed;
8077

@@ -191,6 +188,9 @@ public Function CreateFunction( ref readonly DIBuilder diBuilder, LazyEncodedStr
191188
/// <inheritdoc/>
192189
public Module Clone( IContext targetContext ) => Impl.Clone( targetContext );
193190

191+
/// <inheritdoc/>
192+
public bool StripDebugInformation() => Impl.StripDebugInformation();
193+
194194
/// <inheritdoc/>
195195
public LazyEncodedString SourceFileName { get => Impl.SourceFileName; set => Impl.SourceFileName = value; }
196196

@@ -236,6 +236,9 @@ public Function CreateFunction( ref readonly DIBuilder diBuilder, LazyEncodedStr
236236

237237
/// <inheritdoc/>
238238
public LazyEncodedString ModuleInlineAsm { get => Impl.ModuleInlineAsm; set => Impl.ModuleInlineAsm = value; }
239+
240+
/// <inheritdoc/>
241+
public uint DebugMetadataVersion => Impl.DebugMetadataVersion;
239242
#endregion
240243

241244
/// <summary>Load a bit-code module from a given file</summary>

0 commit comments

Comments
 (0)