Skip to content

Commit

Permalink
Force C/C++ code to be formatted by GitHub Actions (#158)
Browse files Browse the repository at this point in the history
* Remove extra .clang-format
* Use better clang-format settings.
* 🤖 apply linter changes (will not trigger CI)
* Only operate in-place if we can push the result.
* Fix templated macros.
  • Loading branch information
alexreinking authored Feb 15, 2022
1 parent f5694b9 commit 287f16b
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 445 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
BasedOnStyle: LLVM
AlignAfterOpenBracket: DontAlign
SpacesBeforeTrailingComments: 2
ConstructorInitializerAllOnOneLineOrOnePerLine: true
AlignAfterOpenBracket: DontAlign
AlwaysBreakTemplateDeclarations: Yes
23 changes: 23 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI
on:
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
- uses: DoozyX/clang-format-lint-action@v0.13
with:
source: '.'
exclude: './dependencies ./tests/gemmini/gemmini-rocc-tests'
clangFormatVersion: 12
inplace: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
# Only push to internal PRs
- uses: EndBug/add-and-commit@v4
if: github.event.pull_request.head.repo.full_name == github.repository
with:
message: '🤖 apply linter changes (will not trigger CI)'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97 changes: 51 additions & 46 deletions SYS_ATL/libs/gemm_acc_malloc.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// clang-format off
// these are filled in by Python's str.format()
#define HEAP_SIZE {heap_size}
#define DIM {dim}
// clang-format on

#include "include/gemmini.h"
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

typedef struct __attribute__((__packed__)) AccBlock {
uint32_t size;
uint32_t loc;
uint8_t is_used;
uint32_t size;
uint32_t loc;
uint8_t is_used;
} AccBlock;

// maintain a stack of blocks corresponding to
Expand All @@ -19,57 +22,59 @@ AccBlock ACC_BLOCKS[N_ACC_BLOCKS];
uint32_t gemm_acc_free_block;

void gemm_acc_init_mem() {
uint8_t *buf = (uint8_t*)ACC_BLOCKS;
for(uint32_t i=0; i<sizeof(ACC_BLOCKS); i++)
buf[i] = 0;
gemm_acc_free_block = 0;
uint8_t *buf = (uint8_t *)ACC_BLOCKS;
for (uint32_t i = 0; i < sizeof(ACC_BLOCKS); i++)
buf[i] = 0;
gemm_acc_free_block = 0;
}

uint32_t gemm_acc_malloc(long unsigned int size) {
// must have two free metadata blocks and
// this allocation must have > 0 size
if(size == 0) return -1;
if(gemm_acc_free_block >= N_ACC_BLOCKS) return -1;
// must have two free metadata blocks and
// this allocation must have > 0 size
if (size == 0)
return -1;
if (gemm_acc_free_block >= N_ACC_BLOCKS)
return -1;

size = (size + DIM - 1) / DIM;
uint32_t i = gemm_acc_free_block;
size = (size + DIM - 1) / DIM;
uint32_t i = gemm_acc_free_block;

uint32_t loc = 0;
if(i > 0) {
loc = ACC_BLOCKS[i-1].loc + ACC_BLOCKS[i-1].size;
}
ACC_BLOCKS[i].size = size;
ACC_BLOCKS[i].loc = 0;
ACC_BLOCKS[i].is_used = 1;
gemm_acc_free_block = i+1;
uint32_t loc = 0;
if (i > 0) {
loc = ACC_BLOCKS[i - 1].loc + ACC_BLOCKS[i - 1].size;
}
ACC_BLOCKS[i].size = size;
ACC_BLOCKS[i].loc = 0;
ACC_BLOCKS[i].is_used = 1;
gemm_acc_free_block = i + 1;

return (ACC_BLOCKS[i].loc | ((uint32_t)0x80000000));
return (ACC_BLOCKS[i].loc | ((uint32_t)0x80000000));
}

void gemm_acc_free(uint32_t addr) {
if( gemm_acc_free_block == 0 )
return;
addr = addr & (uint32_t)(0x7FFFFFFF);
// first case: free-ing the top of the block-stack
if( ACC_BLOCKS[gemm_acc_free_block-1].loc == addr ) {
ACC_BLOCKS[gemm_acc_free_block-1].is_used = 0;
if (gemm_acc_free_block == 0)
return;
addr = addr & (uint32_t)(0x7FFFFFFF);
// first case: free-ing the top of the block-stack
if (ACC_BLOCKS[gemm_acc_free_block - 1].loc == addr) {
ACC_BLOCKS[gemm_acc_free_block - 1].is_used = 0;

// Then go through and release as many blocks
// as we can
for(int i=gemm_acc_free_block-1; i >= 0; i--) {
if(ACC_BLOCKS[i].is_used)
break; // loop termination
// otherwise...
gemm_acc_free_block = i;
}
// Then go through and release as many blocks
// as we can
for (int i = gemm_acc_free_block - 1; i >= 0; i--) {
if (ACC_BLOCKS[i].is_used)
break; // loop termination
// otherwise...
gemm_acc_free_block = i;
}
// second case: find the freed block and mark it
} else {
for(int i=gemm_acc_free_block-1; i >= 0; i--) {
if(ACC_BLOCKS[i].loc == addr) {
ACC_BLOCKS[i].is_used = 0;
break;
}
}
} else {
for (int i = gemm_acc_free_block - 1; i >= 0; i--) {
if (ACC_BLOCKS[i].loc == addr) {
ACC_BLOCKS[i].is_used = 0;
break;
}
}
return;
}
return;
}
66 changes: 36 additions & 30 deletions SYS_ATL/libs/gemm_malloc.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
// clang-format off
// these are filled in by Python's str.format()
#define HEAP_SIZE {heap_size}
#define DIM {dim}
// clang-format on

#include "include/gemmini.h"
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

typedef struct __attribute__((__packed__)) NewBlock {
uint32_t size;
uint32_t loc;
uint8_t is_used;
uint32_t size;
uint32_t loc;
uint8_t is_used;
} NewBlock;

NewBlock BLOCKS[HEAP_SIZE / sizeof(NewBlock)];
uint32_t gemm_last_ptr;

void gemm_init_mem() {
for(uint32_t i=0; i<sizeof(BLOCKS); i++)
((uint8_t*)BLOCKS)[i] = 0;
gemm_last_ptr = 0;
for (uint32_t i = 0; i < sizeof(BLOCKS); i++)
((uint8_t *)BLOCKS)[i] = 0;
gemm_last_ptr = 0;
}

uint32_t gemm_malloc(long unsigned int size) {
if(size == 0) return -1;
size = (size + DIM - 1) / DIM;
int i;
for(i=0; i < HEAP_SIZE / sizeof(NewBlock) && BLOCKS[i].size > 0; i++) {
if(BLOCKS[i].is_used) continue;
if(BLOCKS[i].size < size) continue;
break;
}
if(BLOCKS[i].size == 0) {
BLOCKS[i].loc = gemm_last_ptr;
BLOCKS[i].size = size;
BLOCKS[i].is_used = 1;
gemm_last_ptr += size;
return BLOCKS[i].loc;
}

if (size == 0)
return -1;
size = (size + DIM - 1) / DIM;
int i;
for (i = 0; i < HEAP_SIZE / sizeof(NewBlock) && BLOCKS[i].size > 0; i++) {
if (BLOCKS[i].is_used)
continue;
if (BLOCKS[i].size < size)
continue;
break;
}
if (BLOCKS[i].size == 0) {
BLOCKS[i].loc = gemm_last_ptr;
BLOCKS[i].size = size;
BLOCKS[i].is_used = 1;
gemm_last_ptr += size;
return BLOCKS[i].loc;
}

BLOCKS[i].is_used = 1;
return BLOCKS[i].loc;
}

void gemm_free(uint32_t addr) {
for(int i=0; BLOCKS[i].size > 0; i++) {
if(BLOCKS[i].is_used && BLOCKS[i].loc == addr) {
BLOCKS[i].is_used = 0;
return;
}
for (int i = 0; BLOCKS[i].size > 0; i++) {
if (BLOCKS[i].is_used && BLOCKS[i].loc == addr) {
BLOCKS[i].is_used = 0;
return;
}
return;
}
return;
}

/*
Expand Down
99 changes: 52 additions & 47 deletions SYS_ATL/libs/malloc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// clang-format off
// these are filled in by Python's str.format()
#define HEAP_SIZE {heap_size}
// clang-format on

#include <stdint.h>
#include <stdio.h>
Expand All @@ -8,70 +11,72 @@ uint8_t HEAP[HEAP_SIZE];

// https://stackoverflow.com/questions/5473189/what-is-a-packed-structure-in-c
typedef struct __attribute__((__packed__)) FreeBlock {
uint32_t size;
struct FreeBlock *next;
uint8_t data[0];
uint32_t size;
struct FreeBlock *next;
uint8_t data[0];
} FreeBlock;
FreeBlock *freelist;

void init_mem() {
FreeBlock *p = (FreeBlock *)HEAP;
p->next = p+1;
p->size = 0;
freelist = p;
FreeBlock *p = (FreeBlock *)HEAP;
p->next = p + 1;
p->size = 0;
freelist = p;

p++;
p->next = 0;
p->size = HEAP_SIZE - sizeof(FreeBlock);
p++;
p->next = 0;
p->size = HEAP_SIZE - sizeof(FreeBlock);
}

void *search(FreeBlock *cur, uint32_t bytes) {
FreeBlock *prev = freelist;
FreeBlock *prev = freelist;

for(;;) {
uint32_t size = sizeof(uint32_t) + sizeof(FreeBlock*);
for (;;) {
uint32_t size = sizeof(uint32_t) + sizeof(FreeBlock *);

if (cur->next == 0 && cur->size < bytes + size) {
printf("Out of memory!\n");
return 0;
} else if (cur->next == 0 && cur->size >= (bytes + size)) {
// cut cur into bytes blocks and create new
uint32_t sz = cur->size;
FreeBlock *new = (void *)cur + bytes + size;
new->next = 0;
new->size = sz - bytes - size;
cur->size = bytes + size;
prev->next = new;
break;
} else if (cur->size >= (bytes + size)) {
prev->next = cur->next;
break;
} else {
prev = cur;
cur = cur->next;
}
if (cur->next == 0 && cur->size < bytes + size) {
printf("Out of memory!\n");
return 0;
} else if (cur->next == 0 && cur->size >= (bytes + size)) {
// cut cur into bytes blocks and create new
uint32_t sz = cur->size;
FreeBlock *new = (void *)cur + bytes + size;
new->next = 0;
new->size = sz - bytes - size;
cur->size = bytes + size;
prev->next = new;
break;
} else if (cur->size >= (bytes + size)) {
prev->next = cur->next;
break;
} else {
prev = cur;
cur = cur->next;
}
}

return cur->data;
return cur->data;
}

void *malloc_dram(long unsigned int bytes) {
bytes = bytes < sizeof(FreeBlock) ? sizeof(FreeBlock) : bytes;
if (bytes == 0) return 0;
FreeBlock *loc = search(freelist, bytes);
if (loc == 0)
return 0;
else {
return loc;
}
bytes = bytes < sizeof(FreeBlock) ? sizeof(FreeBlock) : bytes;
if (bytes == 0)
return 0;
FreeBlock *loc = search(freelist, bytes);
if (loc == 0)
return 0;
else {
return loc;
}
}

void free_dram(void *ptr) {
if (ptr == 0) return;
ptr = ptr - sizeof(uint32_t) - sizeof(FreeBlock*);
FreeBlock *next = freelist->next;
freelist->next = ptr;
((FreeBlock*)ptr)->next = next;

if (ptr == 0)
return;
ptr = ptr - sizeof(uint32_t) - sizeof(FreeBlock *);
FreeBlock *next = freelist->next;
freelist->next = ptr;
((FreeBlock *)ptr)->next = next;

return;
}
Loading

0 comments on commit 287f16b

Please sign in to comment.