Skip to content

Commit e66458a

Browse files
committed
[LLVM-C] [bindings/go] Add C and Golang bindings for COMDAT
Patch by Ben Clayton Differential Revision: https://reviews.llvm.org/D44086 llvm-svn: 327551
1 parent 5a48cf8 commit e66458a

File tree

5 files changed

+186
-10
lines changed

5 files changed

+186
-10
lines changed

llvm/bindings/go/llvm/ir.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package llvm
1515

1616
/*
1717
#include "llvm-c/Core.h"
18+
#include "llvm-c/Comdat.h"
1819
#include "IRBindings.h"
1920
#include <stdlib.h>
2021
*/
@@ -37,6 +38,9 @@ type (
3738
Value struct {
3839
C C.LLVMValueRef
3940
}
41+
Comdat struct {
42+
C C.LLVMComdatRef
43+
}
4044
BasicBlock struct {
4145
C C.LLVMBasicBlockRef
4246
}
@@ -61,14 +65,15 @@ type (
6165
Attribute struct {
6266
C C.LLVMAttributeRef
6367
}
64-
Opcode C.LLVMOpcode
65-
TypeKind C.LLVMTypeKind
66-
Linkage C.LLVMLinkage
67-
Visibility C.LLVMVisibility
68-
CallConv C.LLVMCallConv
69-
IntPredicate C.LLVMIntPredicate
70-
FloatPredicate C.LLVMRealPredicate
71-
LandingPadClause C.LLVMLandingPadClauseTy
68+
Opcode C.LLVMOpcode
69+
TypeKind C.LLVMTypeKind
70+
Linkage C.LLVMLinkage
71+
Visibility C.LLVMVisibility
72+
CallConv C.LLVMCallConv
73+
ComdatSelectionKind C.LLVMComdatSelectionKind
74+
IntPredicate C.LLVMIntPredicate
75+
FloatPredicate C.LLVMRealPredicate
76+
LandingPadClause C.LLVMLandingPadClauseTy
7277
)
7378

7479
func (c Context) IsNil() bool { return c.C == nil }
@@ -248,6 +253,18 @@ const (
248253
X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
249254
)
250255

256+
//-------------------------------------------------------------------------
257+
// llvm.ComdatSelectionKind
258+
//-------------------------------------------------------------------------
259+
260+
const (
261+
AnyComdatSelectionKind ComdatSelectionKind = C.LLVMAnyComdatSelectionKind
262+
ExactMatchComdatSelectionKind ComdatSelectionKind = C.LLVMExactMatchComdatSelectionKind
263+
LargestComdatSelectionKind ComdatSelectionKind = C.LLVMLargestComdatSelectionKind
264+
NoDuplicatesComdatSelectionKind ComdatSelectionKind = C.LLVMNoDuplicatesComdatSelectionKind
265+
SameSizeComdatSelectionKind ComdatSelectionKind = C.LLVMSameSizeComdatSelectionKind
266+
)
267+
251268
//-------------------------------------------------------------------------
252269
// llvm.IntPredicate
253270
//-------------------------------------------------------------------------
@@ -1029,6 +1046,25 @@ func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
10291046
return
10301047
}
10311048

1049+
// Operations on comdat
1050+
func (m Module) Comdat(name string) (c Comdat) {
1051+
cname := C.CString(name)
1052+
defer C.free(unsafe.Pointer(cname))
1053+
c.C = C.LLVMGetOrInsertComdat(m.C, cname)
1054+
return
1055+
}
1056+
1057+
func (v Value) Comdat() (c Comdat) { c.C = C.LLVMGetComdat(v.C); return }
1058+
func (v Value) SetComdat(c Comdat) { C.LLVMSetComdat(v.C, c.C) }
1059+
1060+
func (c Comdat) SelectionKind() ComdatSelectionKind {
1061+
return ComdatSelectionKind(C.LLVMGetComdatSelectionKind(c.C))
1062+
}
1063+
1064+
func (c Comdat) SetSelectionKind(k ComdatSelectionKind) {
1065+
C.LLVMSetComdatSelectionKind(c.C, (C.LLVMComdatSelectionKind)(k))
1066+
}
1067+
10321068
// Operations on functions
10331069
func AddFunction(m Module, name string, ft Type) (v Value) {
10341070
cname := C.CString(name)

llvm/include/llvm-c/Comdat.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*===-- llvm-c/Comdat.h - Module Comdat C Interface -------------*- C++ -*-===*\
2+
|* *|
3+
|* The LLVM Compiler Infrastructure *|
4+
|* *|
5+
|* This file is distributed under the University of Illinois Open Source *|
6+
|* License. See LICENSE.TXT for details. *|
7+
|* *|
8+
|*===----------------------------------------------------------------------===*|
9+
|* *|
10+
|* This file defines the C interface to COMDAT. *|
11+
|* *|
12+
\*===----------------------------------------------------------------------===*/
13+
14+
#ifndef LLVM_C_COMDAT_H
15+
#define LLVM_C_COMDAT_H
16+
17+
#include "llvm-c/Types.h"
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
typedef enum {
24+
LLVMAnyComdatSelectionKind, ///< The linker may choose any COMDAT.
25+
LLVMExactMatchComdatSelectionKind, ///< The data referenced by the COMDAT must
26+
///< be the same.
27+
LLVMLargestComdatSelectionKind, ///< The linker will choose the largest
28+
///< COMDAT.
29+
LLVMNoDuplicatesComdatSelectionKind, ///< No other Module may specify this
30+
///< COMDAT.
31+
LLVMSameSizeComdatSelectionKind ///< The data referenced by the COMDAT must be
32+
///< the same size.
33+
} LLVMComdatSelectionKind;
34+
35+
/**
36+
* Return the Comdat in the module with the specified name. It is created
37+
* if it didn't already exist.
38+
*
39+
* @see llvm::Module::getOrInsertComdat()
40+
*/
41+
LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name);
42+
43+
/**
44+
* Get the Comdat assigned to the given global object.
45+
*
46+
* @see llvm::GlobalObject::getComdat()
47+
*/
48+
LLVMComdatRef LLVMGetComdat(LLVMValueRef V);
49+
50+
/**
51+
* Assign the Comdat to the given global object.
52+
*
53+
* @see llvm::GlobalObject::setComdat()
54+
*/
55+
void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C);
56+
57+
/*
58+
* Get the conflict resolution selection kind for the Comdat.
59+
*
60+
* @see llvm::Comdat::getSelectionKind()
61+
*/
62+
LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C);
63+
64+
/*
65+
* Set the conflict resolution selection kind for the Comdat.
66+
*
67+
* @see llvm::Comdat::setSelectionKind()
68+
*/
69+
void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind Kind);
70+
71+
#ifdef __cplusplus
72+
}
73+
#endif
74+
75+
#endif

llvm/include/llvm-c/Types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ typedef struct LLVMOpaqueAttributeRef *LLVMAttributeRef;
134134
*/
135135
typedef struct LLVMOpaqueDiagnosticInfo *LLVMDiagnosticInfoRef;
136136

137+
/**
138+
* @see llvm::Comdat
139+
*/
140+
typedef struct LLVMComdat *LLVMComdatRef;
141+
137142
/**
138143
* @}
139144
*/

llvm/include/llvm/IR/Comdat.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#ifndef LLVM_IR_COMDAT_H
1717
#define LLVM_IR_COMDAT_H
1818

19+
#include "llvm-c/Types.h"
20+
#include "llvm/Support/CBindingWrapping.h"
21+
1922
namespace llvm {
2023

2124
class raw_ostream;
@@ -55,6 +58,9 @@ class Comdat {
5558
SelectionKind SK = Any;
5659
};
5760

61+
// Create wrappers for C Binding types (see CBindingWrapping.h).
62+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Comdat, LLVMComdatRef)
63+
5864
inline raw_ostream &operator<<(raw_ostream &OS, const Comdat &C) {
5965
C.print(OS);
6066
return OS;

llvm/lib/IR/Comdat.cpp

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
//
88
//===----------------------------------------------------------------------===//
99
//
10-
// This file implements the Comdat class.
10+
// This file implements the Comdat class (including the C bindings).
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "llvm/IR/Comdat.h"
14+
#include "llvm-c/Comdat.h"
1515
#include "llvm/ADT/StringMap.h"
1616
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/IR/Comdat.h"
18+
#include "llvm/IR/GlobalObject.h"
19+
#include "llvm/IR/Module.h"
1720

1821
using namespace llvm;
1922

@@ -22,3 +25,54 @@ Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {}
2225
Comdat::Comdat() = default;
2326

2427
StringRef Comdat::getName() const { return Name->first(); }
28+
29+
LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) {
30+
return wrap(unwrap(M)->getOrInsertComdat(Name));
31+
}
32+
33+
LLVMComdatRef LLVMGetComdat(LLVMValueRef V) {
34+
GlobalObject *G = unwrap<GlobalObject>(V);
35+
return wrap(G->getComdat());
36+
}
37+
38+
void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) {
39+
GlobalObject *G = unwrap<GlobalObject>(V);
40+
G->setComdat(unwrap(C));
41+
}
42+
43+
LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) {
44+
switch (unwrap(C)->getSelectionKind()) {
45+
case Comdat::Any:
46+
return LLVMAnyComdatSelectionKind;
47+
case Comdat::ExactMatch:
48+
return LLVMExactMatchComdatSelectionKind;
49+
case Comdat::Largest:
50+
return LLVMLargestComdatSelectionKind;
51+
case Comdat::NoDuplicates:
52+
return LLVMNoDuplicatesComdatSelectionKind;
53+
case Comdat::SameSize:
54+
return LLVMSameSizeComdatSelectionKind;
55+
}
56+
llvm_unreachable("Invalid Comdat SelectionKind!");
57+
}
58+
59+
void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) {
60+
Comdat *Cd = unwrap(C);
61+
switch (kind) {
62+
case LLVMAnyComdatSelectionKind:
63+
Cd->setSelectionKind(Comdat::Any);
64+
break;
65+
case LLVMExactMatchComdatSelectionKind:
66+
Cd->setSelectionKind(Comdat::ExactMatch);
67+
break;
68+
case LLVMLargestComdatSelectionKind:
69+
Cd->setSelectionKind(Comdat::Largest);
70+
break;
71+
case LLVMNoDuplicatesComdatSelectionKind:
72+
Cd->setSelectionKind(Comdat::NoDuplicates);
73+
break;
74+
case LLVMSameSizeComdatSelectionKind:
75+
Cd->setSelectionKind(Comdat::SameSize);
76+
break;
77+
}
78+
}

0 commit comments

Comments
 (0)