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

Implement module-independent data retrieval operations for LLVMNamedMDNodeRef #995

Merged
merged 5 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/src/main/java/org/bytedeco/llvm/presets/LLVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"<llvm-c/Comdat.h>", "<llvm-c/DebugInfo.h>", "<llvm-c/Error.h>", "<llvm-c/ErrorHandling.h>", "<llvm-c/OrcBindings.h>", "<llvm-c/Remarks.h>",
"<llvm-c/Transforms/AggressiveInstCombine.h>", "<llvm-c/Transforms/Coroutines.h>", "<llvm-c/Transforms/InstCombine.h>",
"<llvm-c/Transforms/IPO.h>", "<llvm-c/Transforms/PassManagerBuilder.h>", "<llvm-c/Transforms/Scalar.h>", "<llvm-c/Transforms/Utils.h>", "<llvm-c/Transforms/Vectorize.h>",
"<polly/LinkAllPasses.h>", "<FullOptimization.h>", "<TargetStubs.h>"},
"<polly/LinkAllPasses.h>", "<FullOptimization.h>", "<TargetStubs.h>", "<NamedMetadataOperations.h>"},
compiler = "cpp14", link = {"LLVM-11", "LTO@.11", "Remarks@.11"}, resource = {"include", "lib"}),
@Platform(value = "macosx", link = {"LLVM", "LTO", "Remarks"}),
@Platform(value = "windows", link = {"LLVM", "LTO", "Remarks"})})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2021 Mats Larsen
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
* the Free Software Foundation (subject to the "Classpath" exception),
* either version 2, or any later version (collectively, the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/
* http://www.gnu.org/software/classpath/license.html
*
* or as provided in the LICENSE.txt file that accompanied this code.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef NAMED_METADATA_OPERATIONS_H
#define NAMED_METADATA_OPERATIONS_H

#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm-c/Core.h"
#include "llvm-c/Types.h"

namespace llvm {

/**
* Exact re-implementation of LLVMGetNamedMetadataNumOperands without providing
* a LLVMModuleRef parameter
*
* Pass a LLVMNamedMDNodeRef instead, which is the true owner for the data
* anyways
*
* See /llvm/lib/IR/Core.cpp for original implementation
*/
extern "C" unsigned NamedMDNodeGetOperandCount(LLVMNamedMDNodeRef NodeRef) {
junlarsen marked this conversation as resolved.
Show resolved Hide resolved
NamedMDNode *N = unwrap(NodeRef);
return N->getNumOperands();
}

/**
* Exact re-implementation of LLVMGetNamedMetadataOperands without providing
* a LLVMModuleRef parameter.
*
* This requires a LLVMContextRef instance because conversion from Metadata to
* Value requires a context in which the new Value will reside in.
*
* See /llvm/lib/IR/Core.cpp for original implementation
*/
extern "C" void NamedMDNodeGetOperands(
LLVMNamedMDNodeRef NodeRef,
LLVMValueRef *Dest,
LLVMContextRef InContext
) {
NamedMDNode *N = unwrap(NodeRef);
LLVMContext *C = unwrap(InContext);
for (unsigned i = 0; i < N->getNumOperands(); i++) {
Dest[i] = wrap(MetadataAsValue::get(*C, N->getOperand(i)));
}
}

/**
* Inlined re-implementation of LLVMAddNamedMetadataOperand without providing
* a LLVMModuleRef parameter.
*
* This implementation inlines the statically defined "extractMDNode" in
* Core.cpp which the original implementation uses.
*
* See /llvm/lib/IR/Core.cpp for original implementation
*/
extern "C" void NamedMDNodeAddOperand(
LLVMNamedMDNodeRef NodeRef,
LLVMValueRef Val
) {
assert(Val && "Expected not-null value Val");

NamedMDNode *N = unwrap(NodeRef);
MetadataAsValue *MAV = unwrap<MetadataAsValue>(Val);
Metadata *MD = MAV->getMetadata();
MDNode* Metadata;
assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) && "Expected a metadata node or a canonicalized constant");

if (MDNode* NN = dyn_cast<MDNode>(MD)) {
Metadata = NN;
}
Metadata = MDNode::get(MAV->getContext(), MD);

N->addOperand(Metadata);
}

} // namespace llvm

#endif // NAMED_METADATA_OPERATIONS_H