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

Use LLVM to detect CPU features by default if --features aren't specified. #1580

Merged
merged 1 commit into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 10 additions & 5 deletions src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,16 @@ bool codegen_init(pass_opt_t* opt)
LLVMInitializeCodeGen(passreg);
LLVMInitializeTarget(passreg);

if(opt->features != NULL)
{
opt->features = LLVMCreateMessage(opt->features);
} else {
if((opt->cpu == NULL) && (opt->triple == NULL))
opt->features = LLVMGetHostCPUFeatures();
else
opt->features = LLVMCreateMessage("");
}

// Default triple, cpu and features.
if(opt->triple != NULL)
{
Expand All @@ -879,11 +889,6 @@ bool codegen_init(pass_opt_t* opt)
else
opt->cpu = LLVMGetHostCPUName();

if(opt->features != NULL)
opt->features = LLVMCreateMessage(opt->features);
else
opt->features = LLVMCreateMessage("");

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/libponyc/codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PONY_EXTERN_C_BEGIN

// Missing from C API.
char* LLVMGetHostCPUName();
char* LLVMGetHostCPUFeatures();
void LLVMSetUnsafeAlgebra(LLVMValueRef inst);
void LLVMSetNoUnsignedWrap(LLVMValueRef inst);
void LLVMSetNoSignedWrap(LLVMValueRef inst);
Expand Down
33 changes: 33 additions & 0 deletions src/libponyc/codegen/host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ char* LLVMGetHostCPUName()
return strdup(sys::getHostCPUName().str().c_str());
}

char* LLVMGetHostCPUFeatures()
{
StringMap<bool> features;
bool got_features = sys::getHostCPUFeatures(features);
assert(got_features);
(void)got_features;

// Calculate the size of buffer that will be needed to return all features.
size_t buf_size = 0;
for(auto it = features.begin(); it != features.end(); it++)
buf_size += (*it).getKey().str().length() + 2; // plus +/- char and ,/null

char* buf = (char*)malloc(buf_size);
assert(buf != NULL);
buf[0] = 0;

for(auto it = features.begin(); it != features.end();)
{
if((*it).getValue())
strcat(buf, "+");
else
strcat(buf, "-");

strcat(buf, (*it).getKey().str().c_str());

it++;
if(it != features.end())
strcat(buf, ",");
}

return buf;
}

void LLVMSetUnsafeAlgebra(LLVMValueRef inst)
{
unwrap<Instruction>(inst)->setHasUnsafeAlgebra(true);
Expand Down
1 change: 1 addition & 0 deletions src/ponyc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ static void usage()
" =name Default is the host CPU.\n"
" --features CPU features to enable or disable.\n"
" =+this,-that Use + to enable, - to disable.\n"
" Defaults to detecting all CPU features from the host.\n"
" --triple Set the target triple.\n"
" =name Defaults to the host triple.\n"
" --stats Print some compiler stats.\n"
Expand Down